Override method

Discussion in 'Rust Development' started by tomlee, Jul 29, 2015.

  1. Hello everyone!
    Sorry if that is dumb question, but i can't find anything about that...
    Is it possible to override method of some entity, or the easiest way will be rewrite file in asset?
    For example: i want to override "Update" method of CargoPlane entity...

    Thank you for help, and sorry for my English!
     
  2. I'm still learning the ropes of .NET and C# (even though I started doing .NET before it was even released, lol) but... as far as I know in order to override a function you need to derive from the class first. Once you've done that, you would need to kill the entity and spawn a new entity with the same values but using your derived class.

    If this works, you'll have another big issue... pretty much none of the game classes are virtual, so you won't be able to override much if anything in there. You could use the new keyword but this would most likely not work for what you want to do as whatever function you want to override is probably called by the game engine itself with a base class reference. There are however a couple of loopholes in C# to get around this specific issue, here is one:

    Code:
        class Test
        {
            public void Execute()
            {
                var a = new RustEntity();
                var b = new HackClassTwo();            a.Update();
                b.Update();
            }        /// <summary>
            /// This is a representation of a Rust class from the game, you cannot change this but you want to...
            /// </summary>
            class RustEntity
            {
                public void Update()
                {
                    MessageBox.Show("Default behavior");
                }
            }        /// <summary>
            /// Here we derrive from the class, but make it abstract.. nothing special here
            /// except we define a new version of Update. Compiler will warn we need to use New
            /// but lets ignore that and see what happens
            /// </summary>
            abstract class HackClassOne : RustEntity
            {
                public abstract void Update();
            }        /// <summary>
            /// This is where the magic happens. By overriding the Update of the abstract class
            /// and having a function of the same name and signature, you totally fool .NET
            /// into thinking this Update is a replacement for the RustEntity class's Update
            /// </summary>
            class HackClassTwo : HackClassOne
            {
                public override void Update()
                {
                    MessageBox.Show("Fooled!");
                }
            }
        }
    
    If that doesn't work, you might want to look at Oxide's code and see how they hook into stuff. I never looked into it, but I assume they are doing pretty much what you want to do. I don't know if you can create new hooks straight into a plugin though, so your project might need a new Oxide hook to be implemented.
     
  3. Deicide666ra, very informative answer! Thank you very much!
    Now i understand where to look further...
     
  4. Umm scratch that code I posted, it's crap. Just realized that I was assigning to a var and not using the base class type to call the function.. and if I do it doesn't work so it's no use! Sorry for the false hope ;)
     
  5. Code scratch is fine... i understand that this is not a solution, i've got right direction to thinking... but especially good idea was to looking on how exactly Oxide made hooks, i'm a newbie in C# programming, but i find few informative articles about IL code injection... now trying to realise that. so it will be a hard way... :)