How to create resource loaders

Topic created · 1 Posts · 59 Views
  • Resources are an important part of the Rhisis project. Without them, it would be difficult to know for exemple, items attributes, monsters names, etc... This is why we need to load them inside the Rhisis server and consume them whenever we need.

    Rhisis has a specific and simple way to load resources. This guide will help you understand how to create new loaders and how to use them.

    Create a simple loader

    To create a loader, you need to create a new class and make it inherit from the Rhisis.Core.Resources.IGameResourceLoader. Once this is done, you will need to implement the following methods to match the interface contract:

    • Load() : Loads the resources
    • Dispose() : Dispose and releases the resources.

    Example:

    using Rhisis.Core.Resources;
    
    public class ItemLoader : IGameResourceLoader
    {
    	public void Load()
    	{
    		// Insert loading code here
    	}
    	
    	public void Dispose()
    	{
    		// Insert dispose code here
    	}
    }
    

    Setting up the loader at startup

    Each server has an entry point and a startup class. The WorldServer for instance, has a WorldServerStartup class that contains all the code necessery for startup. It includes configuration files loading, database setup and resource loading.

    public sealed class WorldServerStartup : IProgramStartup
    {
    	// ...
    	private readonly IEnumerable<Type> _loaders = new []
    	{
    		typeof(DefineLoader),
    		typeof(TextLoader),
    		typeof(ItemLoader), // Item loader added
    		// TODO: add more here
    	};
    
    	public void Configure()
    	{
    		// ...
    		GameResources.Instance.Initialize(this._loaders);
    		DependencyContainer.Instance.BuildServiceProvider();
    	}
    
    	public void Run()
    	{
    		try
    		{
    			GameResources.Instance.Load(); // Call loaders in the order defined in the _loaders field.
    		}
    		catch (Exception e)
    		{
    			// ...
    		}
    	}
    }
    

    In the piece of code shown above, we add the ItemLoader to the _loaders private field of the WorldServerStartup class and then we call the GameResources.Instance.Initialize(this._loaders); which will register every loaders.
    When everything is configured, the program calls the Run() method which will call the GameResources.Instance.Load(); and load every loader in the defined order.

    Working with dependency injection

    You have the possibility to inject dependencies into your loaders, like the logger, or other loaders that have been already loaded.

    using Rhisis.Core.Resources;
    
    public class ItemLoader : IGameResourceLoader
    {
    	private readonly ILogger<ItemLoader> _logger;
    	private readonly DefineLoader _defines;
    
    	public ItemLoader(ILogger<ItemLoader> logger, DefineLoader defines)
    	{
    		this._logger = logger;
    		this._defines = defines;
    	}
    
    	public void Load()
    	{
    		this._logger.LogInformations("Loading items...");
    		// Insert loading code here
    	}
    	
    	public void Dispose()
    	{
    		// Insert dispose code here
    	}
    }