What's new
  • Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Visit Resources
  • Visit Downloads
  • Visit Portal

How change Trinity setting var with my plugin

kleinerMann

Member
Joined
May 27, 2013
Messages
64
Reaction score
1
Hi folks,

i wanna change (read) the Trinity var "Trinity.Settings.Combat.Misc.TrashPackSize" with my own plugin.
Is this possible?

Any ideas?

Greets
kleinerMann
 
Trinity.Trinity.Instance.Settings.Combat.Misc.TrashPackSize

should be it, or something similar
 
Trinity.Trinity.Instance.Settings.Combat.Misc.TrashPackSize
should be it, or something similar

this was my idea too but i didnt get a valid code :(
-> Trinity.Trinity.Settings.Combat.Misc.TrashPackSize

Error: "error CS0103: Der Name 'Trinity' ist im aktuellen Kontext nicht vorhanden."
 
Last edited:
Using Trinity; ?
Sure. Trinity is active an works fine.
If i put my plugin in the same folder like trinity an if i use the same namespace (trinity), my plugin can find and read/change the value.
if i had my own folder and namespace -> error CS0103
 
Sure. Trinity is active an works fine.
If i put my plugin in the same folder like trinity an if i use the same namespace (trinity), my plugin can find and read/change the value.
if i had my own folder and namespace -> error CS0103

Reflection.
 
Reflection.
Thanks for this hint. But i dont know to realise this.
im not the perfect coder.
Could you give me a little example?

Edit: i found the file "TrinityApi.cs" in the helper folder of QuestTools-plugin. i try to understand the reflection in this file.
 
Last edited:
i found the file "TrinityApi.cs" in the helper folder of QuestTools-plugin. i try to understand the reflection in this file.

I expetiment with the TrinityApi.cs but i get always an error. I cant get the Value of the field "_TrashPackSize".
I just can list all Fields of "Trinity.Config.Combat.MiscCombatSetting"

Here my code:
OnPulse() in my plugin.cs
Code:
...
object tpValue = null;
if(TrinityApi.GetField("Trinity.Config.Combat.MiscCombatSetting","_TrashPackSize", out tpValue))
    Logger.Log(tpValue.ToString());
...
my TrinityApi.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace SpeedUpBounties.Helpers // original code from rrrix quest tools
{
    /// <summary>
    /// This class well get and set static Trinity Properties and Fields. It does not support instances.
    /// </summary>
    class TrinityApi
    {
        // Trinity_635346394618921335.dll
        private const string AssemblyName = "Trinity";
        // namespace Trinity, public class Trinity
        private const string MainClass = "Trinity.Trinity";

        private static Assembly _mainAssembly;
        private static Dictionary<Tuple<string, Type>, PropertyInfo> _propertyInfoDictionary = new Dictionary<Tuple<string, Type>, PropertyInfo>();
        private static Dictionary<Tuple<string, Type>, FieldInfo> _fieldInfoDictionary = new Dictionary<Tuple<string, Type>, FieldInfo>();
        private static Dictionary<string, Type> _typeDictionary = new Dictionary<string, Type>();

        public static Dictionary<Tuple<string, Type>, PropertyInfo> PropertyInfoDictionary
        {
            get { return _propertyInfoDictionary; }
            set { _propertyInfoDictionary = value; }
        }

        public static Dictionary<Tuple<string, Type>, FieldInfo> FieldInfoDictionary
        {
            get { return _fieldInfoDictionary; }
            set { _fieldInfoDictionary = value; }
        }

        public static Dictionary<string, Type> TypeDictionary
        {
            get { return _typeDictionary; }
            set { _typeDictionary = value; }
        }

        /// <summary>
        /// Gets a value from a Field
        /// </summary>
        /// <param name="typeName">The Type Name</param>
        /// <param name="memberName">The Member Name</param>
        /// <param name="value">The Value</param>
        /// <returns>If the field was successfully returned</returns>
        internal static bool GetField(string typeName, string memberName, out object value)
        {
            value = null;
            try
            {
                Type targetType;
                if (!SetType(typeName, out targetType))
                {
                    return false;
                }
                if (targetType == null)
                    return false;

                FieldInfo fieldInfo;
                if (_fieldInfoDictionary.TryGetValue(new Tuple<string, Type>(memberName, targetType), out fieldInfo))
                {
                    value = fieldInfo.GetValue(null);
                    return true;
                }

                fieldInfo = targetType.GetField(memberName,BindingFlags.NonPublic | BindingFlags.Instance); // [kleinerMann] add ",BindingFlags.NonPublic | BindingFlags.Instance" to get private fields
                if (fieldInfo != null)
                {
                    _fieldInfoDictionary.Add(new Tuple<string, Type>(memberName, targetType), fieldInfo);

                    value = fieldInfo.GetValue(null);
                    return true;
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Error reading Property {0} from Type {1} - {2}", memberName, typeName, ex.Message);
                return false;
            }
            return false;
        }

        /// <summary>
        /// Caches the given type by name
        /// </summary>
        /// <param name="name">The Type name</param>
        /// <param name="result">The Type</param>
        /// <returns>If the type was found</returns>
        private static bool SetType(string name, out Type result)
        {
            result = null;
            try
            {
                SetAssembly();

                if (_mainAssembly == null)
                {
                    return false;
                }

                if (TypeDictionary.TryGetValue(name, out result) && result != null)
                    return true;

                result = _mainAssembly.GetType(name);

                if (TypeDictionary.ContainsKey(name))
                    TypeDictionary[name] = result;
                else
                    TypeDictionary.Add(name, result);

                return true;

            }
            catch (Exception ex)
            {
                Logger.Log("Unable to read type {0} from Assembly: {2}", name, ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Caches the Assembly
        /// </summary>
        private static void SetAssembly()
        {
            try
            {
                if (_mainAssembly != null)
                    return;

                foreach (var asm in AppDomain.CurrentDomain.GetAssemblies().Where(x => x.GetName().Name.StartsWith(AssemblyName)).Where(asm => asm != null))
                {
                    try
                    {
                        Type T = asm.GetType(MainClass);
                        if (T != null) //[kleinerMann] added to verify the correct assembly
							_mainAssembly = asm;
                    }
                    catch
                    {
                        // Not the Plugin, probably the routine, keep trying
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Unable to set Assembly {2}", ex.Message);
            }
        }
    }
}

i get the correct field with
Code:
fieldInfo = targetType.GetField(memberName,BindingFlags.NonPublic | BindingFlags.Instance);
but
Code:
value = fieldInfo.GetValue(null);
throw an exeption: "Non-static field requires a target."
which is the correct code to get the value? which object instead null must i use?

Pls help me community-coders, u r my last hope :)
 
lol... i found the solution in toNyx TrinityCOM of [Plugin] GearSwap Reloaded

I add in "internal static bool GetField(string typeName, string memberName, out object value)"
Code:
object instance = Activator.CreateInstance(targetType);
and later i use
Code:
value = fieldInfo.GetValue(instance);

thanks rrrix and toNyx for your "api" files with reflection code include.

full GetField()
Code:
        internal static bool GetField(string typeName, string memberName, out object value)
        {
            value = null;
            try
            {
                Type targetType;
                if (!SetType(typeName, out targetType))
                {
                    return false;
                }
                if (targetType == null)
                    return false;

                object instance = Activator.CreateInstance(targetType);
                FieldInfo fieldInfo;
                if (_fieldInfoDictionary.TryGetValue(new Tuple<string, Type>(memberName, targetType), out fieldInfo))
                {
                    value = fieldInfo.GetValue(instance);
                    return true;
                }

                FieldInfo[] F = targetType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
                fieldInfo = targetType.GetField(memberName,BindingFlags.NonPublic | BindingFlags.Instance);
                if (fieldInfo != null)
                {
                    _fieldInfoDictionary.Add(new Tuple<string, Type>(memberName, targetType), fieldInfo);

                    value = fieldInfo.GetValue(instance);
                    return true;
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Error reading Property {0} from Type {1} - {2}", memberName, typeName, ex.Message);
                return false;
            }
            return false;
        }
 
Last edited:
lol... i found the solution in toNyx TrinityCOM of [Plugin] GearSwap Reloaded

I add in "internal static bool GetField(string typeName, string memberName, out object value)"
Code:
object instance = Activator.CreateInstance(targetType);
and later i use
Code:
value = fieldInfo.GetValue(instance);

thanks rrrix and toNyx for your "api" files with reflection code include.

full GetField()
Code:
        internal static bool GetField(string typeName, string memberName, out object value)
        {
            value = null;
            try
            {
                Type targetType;
                if (!SetType(typeName, out targetType))
                {
                    return false;
                }
                if (targetType == null)
                    return false;

                object instance = Activator.CreateInstance(targetType);
                FieldInfo fieldInfo;
                if (_fieldInfoDictionary.TryGetValue(new Tuple<string, Type>(memberName, targetType), out fieldInfo))
                {
                    value = fieldInfo.GetValue(instance);
                    return true;
                }

                FieldInfo[] F = targetType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
                fieldInfo = targetType.GetField(memberName,BindingFlags.NonPublic | BindingFlags.Instance);
                if (fieldInfo != null)
                {
                    _fieldInfoDictionary.Add(new Tuple<string, Type>(memberName, targetType), fieldInfo);

                    value = fieldInfo.GetValue(instance);
                    return true;
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Error reading Property {0} from Type {1} - {2}", memberName, typeName, ex.Message);
                return false;
            }
            return false;
        }

;) This board needs more curious people like you ! Glad you found out :)
 
Last edited:
so, i can now read the fields and properties from Trinity.
anyone hat an idea to set values to fields and properties?

this didnt work, cause it change the value in the instance of my plugin and didnt overwrite the value in Trinity.
Code:
...
object instance = Activator.CreateInstance(targetType);
...
propertyInfo.SetValue(instance, value, null);
...

how can i get the instance of Trinity to manipulate the values?
 
Damn... i just can read the (default) values of my new created instance... :(

any way to get the original trinity instance?
board coders or staff, pls help me :)

Yep, Create a "transition" in your trinity folder, like this :

Code:
using Zeta.Bot.Profile;
using Zeta.TreeSharp;
using Zeta.XmlEngine;

namespace Trinity
{
    public class GearSwapMagic
    {
		public static int ArcaneRadiusToRestore;
		public static int AzmoFireBallRadiusToRestore;
	
        public static void DisableRadius(AvoidanceType type)
		{
			switch(type)
			{
				case AvoidanceType.Arcane:
					ArcaneRadiusToRestore = Trinity.Settings.Combat.AvoidanceRadius.Arcane;
					Trinity.Settings.Combat.AvoidanceRadius.Arcane = 2;
					break;
				case AvoidanceType.AzmoFireball:
					AzmoFireBallRadiusToRestore = Trinity.Settings.Combat.AvoidanceRadius.AzmoFireBall;
					Trinity.Settings.Combat.AvoidanceRadius.AzmoFireBall = 2;
					break;
				case AvoidanceType.AzmodanBody:
					break;
			}
			
			Trinity.Settings.Save();
		}
		
		public static void RestoreRadius(AvoidanceType type)
		{
			switch(type)
			{
				case AvoidanceType.Arcane:
					Trinity.Settings.Combat.AvoidanceRadius.Arcane = ArcaneRadiusToRestore;
					break;
				case AvoidanceType.AzmoFireball:
					Trinity.Settings.Combat.AvoidanceRadius.AzmoFireBall = AzmoFireBallRadiusToRestore;
					break;
				case AvoidanceType.AzmodanBody:
					break;	
			}
			
			Trinity.Settings.Save();
		}
    }
}

So you hook your class and call methods/parameters from your plugin.
 
Yes if you can retrieve the exact type of what you're searching for ;)
i wanna read and change the TrashPackSize. My plan is creating a plugin to speedup the Bounty runs.
- im in a area in which i dont have to kill mobs -> TrashPackSize 6
- im in a area in which i have to kill all mobs -> TrashPackSize 1

i already have a plugin-file to place in Trinity folder and it works ok but i want a own folder.
any ideas?
 
i wanna read and change the TrashPackSize. My plan is creating a plugin to speedup the Bounty runs.
- im in a area in which i dont have to kill mobs -> TrashPackSize 6
- im in a area in which i have to kill all mobs -> TrashPackSize 1

i already have a plugin-file to place in Trinity folder and it works ok but i want a own folder.
any ideas?

Create your plugin and use reflection to access the file you created in Trinity's folder.

Code:
public static void SetTrashPackSize(int value)
{
	// Your thing to modify trashpack (eg. Trinity.Settings.Misc.TrashPackSize = value;)
}

And use GetMethod("SetTrashPackSize") to invoke it afterwards
 
Last edited:
access the file you created in Trinity's folder.
This is the problem. im searching for a solution without a additional file in trinity folder.

a already have a plugin with file in trinity folder.
but if i update Trinity with EZUpdater my file would deleted an my plugin must copy a new file to Tinity.

i want to code a solution without the additional file. its a little training for me. i chck all types in trinity but i dont know how to find the correct istance of trinity with the values.
 
Back
Top