Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

[Suggestion] Add System.Dynamic to whitelist

Discussion in 'Programming Questions and Suggestions' started by pipakin, Jan 9, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. pipakin Apprentice Engineer

    Messages:
    182
    It would allow us to do things like the following:

    Code:
    void Main()
    {
        dynamic block = new BlockEx(GridTerminalSystem.GetBlockWithName("Antenna"));
        block.OnOff_Off();
    
        dynamic connector = new BlockEx(GridTerminalSystem.GetBlockWithName("Connector"));
        connector.SwitchLock();
    }
    
    class BlockEx : System.Dynamic.DynamicObject
    {
        private IMyTerminalBlock _block;
    
        public BlockEx(IMyTerminalBlock block)
        {
            _block = block;
        }
     
            
        public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
        {
            result = null;
    
            var action = _block.GetActionWithName(binder.Name);
            if(action == null)
            {
                return false;
            }
            action.Apply(_block);
            return true;
        }
    }
    
    I think there's also an additional reference that has to be made, but this wouldn't give us any exploitable stuff as far as I know, but let us use the dynamic language bits. :)
     
  2. mze9412 Junior Engineer

    Messages:
    791
    Please no dynamic. It removes compile time safety because it only evaluates at runtime if the method or property you try to use really exists and calls it via reflection. Basically "check code" would no longer find typos like "GetActionWihtName". Instead an exception will be thrown when you press "Run".

    And also performance might be impacted a bit by it. On all my years as a c# developer dynamic was never worth the possible trouble it might lead to.
     
  3. pipakin Apprentice Engineer

    Messages:
    182
    Dynamic doesn't affect the whole thing, just variables you explicitly define as dynamic. It doesn't remove all compile time safety. Besides, you can use the dynamic keyword now, you just can't define mappings because the namespace isn't included.

    And in my example, it would be exactly equivalent to misspelling the action name, which can't be checked at compile time either.
     
  4. mze9412 Junior Engineer

    Messages:
    791
    I would prefer dynamic being explicitly forbidden in ingame scripts ;)

    Yeah, the actions also kinda suck the way they are implemented :\
     
  5. pipakin Apprentice Engineer

    Messages:
    182
    And I'm for it being opened for use. I think we'll have to agree to disagree. I think the DLR is quite a useful tool for the right applications. I've been using it since c# 4.0 was released, and it can be a godsend when dealing with weird 3rd party APIs. It's also great for mocking, if you don't have access to a good mocking framework.

    In this case, I'd like to have access to it to wrap an RPC api so I wouldn't be stringly (sic) typing everything, and so the code to use the api can be more natural.
     
  6. Phoera Senior Engineer

    Messages:
    1,713
    in this case good solution is make api better, than enable dynamic.(make setter to set values and methods to trigger actions)
    also dont forget that dynamic need reflection, which is also forbidden.(and must be here)

    btw you now can handle api better if use extension methods.(i hope they are working)
    ---------------------------------------------------------

    Checked, extension methods dont work, bad luck:(
     
    Last edited by a moderator: Jan 12, 2015
  7. Cuber Apprentice Engineer

    Messages:
    262
    They do with a bit of trickery ;)
    http://forums.keenswh.com/post?id=7225801
     
  8. Phoera Senior Engineer

    Messages:
    1,713
    hm, yes, this will work, as i understand developers will not close this, cuz it will not harm anyone.
    (strange that i don't figure out it itself, cuz i know how this work >< )

    btw, are the same script compiled once for all blocks? or every block always have different assemblies?
    -------------------------------------------------------------------------------------
    Looked in delphs of game, cannot see any cache, so looks like all programmable blocks have different assemblies and static variable will not give any more functional than current variables of class Program, so it looks good working thing.
     
    Last edited by a moderator: Jan 12, 2015
  9. pipakin Apprentice Engineer

    Messages:
    182
    Enabling dynamic would not require enabling reflection, as the script itself does not use the reflection types, that happens deep in the DLR. There are plenty of available types already that use reflection internally. You'd be suprised at the amount of reflection that gets triggered. And again, the use I wanted to use it for had nothing to do with actions, that was just an example.

    I want to create an RPC system that doesn't have tons of magic strings in it. I need dynamic types to do that. I'm not sure I understand the dislike for it.
     
  10. Phoera Senior Engineer

    Messages:
    1,713
    i understand. but i prefer that we have simple
    Rotor.Reverse method.

    also dont forget that instructions count are limited. so additional instructions is not good.

    i think devs can enable to use dynamic, but dont do any api dynamics then.
     
    Last edited by a moderator: Jan 12, 2015
  11. tyrsis Junior Engineer

    Messages:
    862
    While I can appreciate what you want to do, it's just not going to happen in Ingame, or even ModAPI. You will definitely be able to do this out of the sandbox though.
     
  12. pipakin Apprentice Engineer

    Messages:
    182
    *shrug* I suppose it's up to the devs. There isn't any reason, other than just not wanting to, not to do it.

    If you are referring to the RPC stuff, I already have that working, I just would like to get a better API.
     
Thread Status:
This last post in this thread was made more than 31 days old.