1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

API.exported (with special resource name)

Discussion in 'Snippets' started by SatsuiBird, May 3, 2017.

  1. SatsuiBird

    SatsuiBird New Member

    Messages:
    1
    Likes Received:
    1
    Joined:
    Apr 29, 2017
    Hello !
    I like to create all my resources in a sub directory and name them like : myname-resourcename. But when i was trying to get exported functions, the result was something like this : API.exported.thugcity/satsui-core(50);
    So, maybe there is a better solution but i created a small class using reflection to get all exported functions. Then i can access it with : Exports.Call("thugcity/satsui-core", "DoSomething", 50);

    You can download the C# file below.

    Code:
    
    /**
     * Helper to get exported functions (even with special resource name)
     * Created by satsuibird@messatsu-dojo.com
     * Exemple : Exports.Call("thugcity/satsui-core", "DoSomething", 50);
     */
    
    using System.Collections.Generic;
    using System.Reflection;
    using GTANetworkServer;
    
    static public class Exports
    {
        static private GtaScript gtaScript = new GtaScript();
        static private List<ExportsResource> resources = new List<ExportsResource>();
    
        #region Call 
    
        static public object Call(string resource, string function, params object[] args)
        {
            ExportsFunction fun = GetFunction(resource, function);
            if (fun == null) return null;
    
            object[] funArgs = new object[] { args };
            object funResult = fun.MethodInfo.Invoke(fun.Delegate, funArgs);
    
            return funResult;
        }
    
        #endregion
    
    
        #region ListResources
    
        static public void ListResources()
        {
            IDictionary<string, object> resources = (IDictionary<string, object>)gtaScript.API.exported;
            foreach (KeyValuePair<string, object> prop in resources)
            {
                gtaScript.API.consoleOutput("(Exports.Resource) " + prop.Key);
            }
        }
    
        #endregion
    
    
        #region ListResourceFunctions
    
        static public void ListResourceFunctions(string resource)
        {
            ExportsResource res = GetResource(resource);
            if (res == null) return;
    
            foreach(ExportsFunction function in res.Functions) gtaScript.API.consoleOutput("(Exports.Function) " + res.Name + "." + function.Name);
        }
    
        #endregion
    
    
        #region GetResource
    
        static public ExportsResource GetResource(string name)
        {
            // Getting resources already called
    
            foreach (ExportsResource res in resources) if (res.Name == name) return res;
    
            // Creating new resource
    
            gtaScript.API.consoleOutput("Creating new resource : " + name);
    
            IDictionary<string, object> dictResources = (IDictionary<string, object>)gtaScript.API.exported;
            object resourceProperty;
            if (!dictResources.TryGetValue(name, out resourceProperty)) return null;
    
            ExportsResource newResource = new ExportsResource(name);
            resources.Add(newResource);
    
            // Listing exported functions of the resource
    
            IDictionary<string, object> exportedFunctions = (IDictionary<string, object>)resourceProperty;
            foreach (KeyValuePair<string, object> prop in exportedFunctions)
            {
                var del = prop.Value as ExportedFunctionDelegate;
                gtaScript.API.consoleOutput(del.ToString());
    
                MethodInfo methodInfo = del.Method;
                newResource.Functions.Add(new ExportsFunction(prop.Key, del, methodInfo));
            }
    
    
            return newResource;
        }
    
        #endregion
    
    
        #region GetFunction
    
        static public ExportsFunction GetFunction(string resource, string name)
        {
            ExportsResource res = GetResource(resource);
            if (res == null) return null;
    
            foreach(ExportsFunction function in res.Functions)
            {
                if (function.Name == name) return function;
            }
            return null;
        }
    
        #endregion
    
    
    }
    
    
    #region GtaScript
    
    public class GtaScript : Script
    {
    }
    
    #endregion
    
    
    #region ExportsResource
    
    public class ExportsResource
    {
        public string Name { get; private set; }
        public List<ExportsFunction> Functions { get; private set; }
    
        public ExportsResource(string name)
        {
            this.Name = name;
            this.Functions = new List<ExportsFunction>();
        }
    }
    
    #endregion
    
    
    #region ExportsFunction
    
    public class ExportsFunction
    {
    
        public string Name { get; private set; }
        public ExportedFunctionDelegate Delegate { get; private set; }
        public MethodInfo MethodInfo { get; private set; }
           
        public ExportsFunction(string name, ExportedFunctionDelegate del, MethodInfo method)
        {
            this.Name = name;
            this.Delegate = del;
            this.MethodInfo = method;
        }
    }
    
    #endregion
    
    
    
    
     

    Attached Files:

    rum likes this.

Share This Page