Go Back   Flash Flash Revolution > General Discussion > Technology
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 10-15-2016, 02:50 PM   #1
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Reflection.Emit

Give me your thoughts on it please. I'm currently studying it on my own for a project of mine and just wanted to hear more about what people think of it.

OpCodes is giving me some trouble since there is so many damn options and I'm not so good at memory management and such low-level skills.

Also, I've heard Expression.Compile is great too but not as versatile and it's limited by lambda usage. Yes/no ? In what context to pick one or the other ?

Thanks
xXOpkillerXx is offline   Reply With Quote
Old 10-15-2016, 05:39 PM   #2
kommisar
Dark Chancellor
Retired StaffFFR Simfile AuthorFFR Music Producer
 
kommisar's Avatar
 
Join Date: Jun 2005
Location: Moncton, NB
Age: 33
Posts: 7,301
Send a message via AIM to kommisar Send a message via MSN to kommisar
Default Re: Reflection.Emit

are there more versatile or useful classes you can use for the same function or is this as good as it gets
__________________

Last edited by kommisar; 10-15-2016 at 05:41 PM..
kommisar is offline   Reply With Quote
Old 10-15-2016, 06:04 PM   #3
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default Re: Reflection.Emit

Wow I was writing a post and my ipad deleted it before I submitted :(

I'm not too familiar with it in c#, but reflection is pretty useful in other languages.. Basically, it allows for you to dynamically create functions, objects etc, and create them or change them during execution, and the program will be able to interact with them as though they were native parts of the code. It also allows you to expose certain elements to external code. A pretty solid practical example of this is runescape bots - imagine rewriting the method that displays the game itself to also output data on objects that are appearing in the world, such as their coordinates.

On the topic of expression compile.. I am predicting one benefit of not using it is because of how arbitrary it could be.. You wouldn't want someone to be able to arbitrarily add code, so perhaps using other methods allows for things such as ensuring it is valid, ensuring it comes from a legit source etc..

I'm reading more on this IS stuff.. It seems like the idea is to have code that is system and code independant, hence why you'd be writing op codes and manually managing the stack, that way it's irrelevant which language executes your IS packag or where it's run.

Regarding the op code stuff, you are basically manually managing a stack.. For example if you want to print letters, you can't just call a print statement, as that would be platform dependant.. Instead, you need to manually place the characters on the stack, then pop them to the output buffer etc.. How familiar are you with data structures and basic assembly stuff (things like registers, subroutines, traps etc).. I'd recommend researching some assembly, how it works in the CPU etc... I'm imagine since you are trying reflection you are familiar with this stuff already though :p

From what I can see, reflection.emit is just an abstraction of writing this op code stuff manually, essentially it converts to machine code anyways... Or maybe I'm just talking out my ass I know literally nothing ;D
Dinglesberry is offline   Reply With Quote
Old 10-15-2016, 06:25 PM   #4
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Re: Reflection.Emit

Thanks for the reply!

Well I'm definitely thinking about writing some kind of utility class that will ensure the validity of the stack manipulations cuz I don't see any way to make sure you always use the right opcodes -.-
I'm not sure how you'd use reflection (.emit at least) to manipulate runescape UI unless you actually have some kind of access to its source code or something (Idk runescape much lol), since that'd be cross-domain level and well there's most likely some security there. In any case I'm not that good at it yet.

I was thinking about starting some program that'd basically take text (that you'd write on UI) and convert that into code, then run it live. Ofc I would only be implementing the basic structures such as simple types (primitives only first), no delegates, maybe a few simple events (??), etc.
xXOpkillerXx is offline   Reply With Quote
Old 10-15-2016, 06:35 PM   #5
Soundwave-
Carry your failures proud
FFR Veteran
 
Soundwave-'s Avatar
 
Join Date: Sep 2015
Age: 23
Posts: 644
Default Re: Reflection.Emit

I think the main concern with Reflection.Emit is that it has a narrow use case.

If you need to write IL code for some reason, with the only use pretty much being a compiler or generator of some description, then Reflection.Emit is great, because it lowers the complexity required to write IL code dynamically.

The other conceivable use case of writing IL code reflects the use of similar constructs in Java: modifying a third-party method at runtime. Unfortunately, C# makes this range from "you really, REALLY, shouldn't be doing this what so ever" to impossible, depending on the exact use case. A shame really.

Expression.Compile seems to be (I probably should have mentioned I'm not qualified to speak definitively on this at all) a shortcut for writing IL should you already have C# expressions. Examples of when this would be the case are writing a C# compiler in C#, or when you are generating code based on some higher level parameters, and it'd be easier to just write it in C# expressions as compared to writing the IL yourself.

In terms of Expression.Compile vs Reflection.Emit, there doesn't seem to be anything indicating to me that either one can generate something that is functionally different from the other, but Expression.Compile guarantees something that functions a certain way where as Reflection.Emit guarantees something that *is* a certain way, which if you're writing IL code for say, performance reasons, could be of interest.
__________________
Quote:
Originally Posted by [11:38 PM] Hakulyte
only person who can legit tilt me is like YoshL
Quote:
Originally Posted by スンファンさん
右に3回回らない限り間違います。
Soundwave- is offline   Reply With Quote
Old 10-15-2016, 06:38 PM   #6
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default Re: Reflection.Emit

Op, if I'm correct from what I can see, this seems like its somewhat similar to what you want to do https://github.com/Ineedajob/RSBot/t...bot/loader/asm

It sounds like you are trying to write an interpreter for plaintext to code? Better start reading about abstract syntax trees :p

The bots work because regardless of the source code or not, java runs on its own virtual machine which I'd imagine is being analyzed, unsure exactly how it works though :(

Last edited by Dinglesberry; 10-15-2016 at 06:40 PM..
Dinglesberry is offline   Reply With Quote
Old 10-15-2016, 06:41 PM   #7
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Re: Reflection.Emit

Quote:
Originally Posted by Soundwave- View Post
Unfortunately, C# makes this range from "you really, REALLY, shouldn't be doing this what so ever" to impossible, depending on the exact use case. A shame really.
Thanks for the reply,

I'm curious to see if you'd have any examples to support this quote ? If that's really the case then I might give it a try in Java since I have no problem using it.
xXOpkillerXx is offline   Reply With Quote
Old 10-15-2016, 06:41 PM   #8
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default Re: Reflection.Emit

Might be interesting reading, seems like it is more general than about a specific language

https://ruslanspivak.com/lsbasi-part1/
Dinglesberry is offline   Reply With Quote
Old 10-15-2016, 06:49 PM   #9
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Re: Reflection.Emit

Quote:
Originally Posted by Dinglesberry View Post
Op, if I'm correct from what I can see, this seems like its somewhat similar to what you want to do https://github.com/Ineedajob/RSBot/t...bot/loader/asm

It sounds like you are trying to write an interpreter for plaintext to code? Better start reading about abstract syntax trees :p

The bots work because regardless of the source code or not, java runs on its own virtual machine which I'd imagine is being analyzed, unsure exactly how it works though :(
Hmmm yeah, after a quick search it seems like there are programs/libraries to intercept the JVM's instructions. Pretty neat (I guess haha)

As for the git repo I might read that a little bit but I'm sticking with c# if there's a possibility I can get it done with that. :)

Quote:
Originally Posted by Dinglesberry View Post
Might be interesting reading, seems like it is more general than about a specific language

https://ruslanspivak.com/lsbasi-part1/
Thanks but no haha, I'm not writting a full on interpreter, got no time to think about all those syntax rules. I know what that requires and I'm not so comfortable with the tasks to accomplish. :p

Last edited by xXOpkillerXx; 10-15-2016 at 06:55 PM..
xXOpkillerXx is offline   Reply With Quote
Old 10-15-2016, 09:21 PM   #10
Soundwave-
Carry your failures proud
FFR Veteran
 
Soundwave-'s Avatar
 
Join Date: Sep 2015
Age: 23
Posts: 644
Default Re: Reflection.Emit

Quote:
Originally Posted by xXOpkillerXx View Post
Thanks for the reply,

I'm curious to see if you'd have any examples to support this quote ? If that's really the case then I might give it a try in Java since I have no problem using it.
See:

http://stackoverflow.com/questions/7...c-sharp-method

Just by doing a bit of pointer work you can replace a "vanilla" method with another "vanilla" method, and there's a link there to really hack it up for other cases. Purportedly it works but it involves native level stuff, and only works on .NET. I couldn't follow it very well myself.

On the other hand, Java lets you really dig into the internals of code loading, making dynamic modification portable and (relatively) easy.

EDIT: It should be noted that both C# and Java suffer from a similar problem in this regard: A launch point. Any loaded code in both cases will not be updated. In C# this is a massive problem because there's no easy way to reload the code that's already loaded, or ensure that you can modify the code before it's loaded. In Java you have multiple options. One is to launch a victim program from another Java program, choosing your custom loading code along the way, or modify the victim program with a class that when loaded, all it does is reload the entire program using custom loading code on a new thread, and kills the current thread.
__________________
Quote:
Originally Posted by [11:38 PM] Hakulyte
only person who can legit tilt me is like YoshL
Quote:
Originally Posted by スンファンさん
右に3回回らない限り間違います。

Last edited by Soundwave-; 10-15-2016 at 10:15 PM..
Soundwave- is offline   Reply With Quote
Old 10-15-2016, 10:23 PM   #11
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Re: Reflection.Emit

?? Not sure if I'm just lacking the knowledge here or not fully understanding the issue you're pointing out, but you can inject a Main(args) easily and run it. Please clarify/explain to me if I'm not getting what you mean. ^^
xXOpkillerXx is offline   Reply With Quote
Old 10-16-2016, 03:36 AM   #12
Soundwave-
Carry your failures proud
FFR Veteran
 
Soundwave-'s Avatar
 
Join Date: Sep 2015
Age: 23
Posts: 644
Default Re: Reflection.Emit

If you're modifying a third-party assembly then your code needs to be ran at some point, requiring that you either be entirely in control of the execution of the assembly or that your code has already been incorporated somehow.
__________________
Quote:
Originally Posted by [11:38 PM] Hakulyte
only person who can legit tilt me is like YoshL
Quote:
Originally Posted by スンファンさん
右に3回回らない限り間違います。
Soundwave- is offline   Reply With Quote
Old 10-16-2016, 07:30 AM   #13
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Re: Reflection.Emit

Quote:
Originally Posted by Soundwave- View Post
If you're modifying a third-party assembly then your code needs to be ran at some point, requiring that you either be entirely in control of the execution of the assembly or that your code has already been incorporated somehow.
Oh yeah absolutely. But that's no flaw to me; if a third party code can be easily modified at runtime it's just a major security flaw if you ask me lmao. But yeah, good thing I only want to modify my own code with reflection. I'll spend some time on it today, I'll let you guys know if I get anything nice ^^
xXOpkillerXx is offline   Reply With Quote
Old 10-16-2016, 03:34 PM   #14
Soundwave-
Carry your failures proud
FFR Veteran
 
Soundwave-'s Avatar
 
Join Date: Sep 2015
Age: 23
Posts: 644
Default Re: Reflection.Emit

Quote:
Originally Posted by xXOpkillerXx View Post
Oh yeah absolutely. But that's no flaw to me; if a third party code can be easily modified at runtime it's just a major security flaw if you ask me lmao. But yeah, good thing I only want to modify my own code with reflection. I'll spend some time on it today, I'll let you guys know if I get anything nice ^^
Well, if you design your code to be modified, both Java and C# have great options for dynamic code. Reflection.Emit allows you to emit to dynamic reflection objects, where as Java let's you be the middle-man in loading, letting you put whatever you want in as well, although .NET seems to have more framework already laid out for you, if you don't want to learn JBC.
__________________
Quote:
Originally Posted by [11:38 PM] Hakulyte
only person who can legit tilt me is like YoshL
Quote:
Originally Posted by スンファンさん
右に3回回らない限り間違います。
Soundwave- is offline   Reply With Quote
Old 10-16-2016, 03:54 PM   #15
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Re: Reflection.Emit

Quote:
Originally Posted by Soundwave- View Post
Well, if you design your code to be modified, both Java and C# have great options for dynamic code. Reflection.Emit allows you to emit to dynamic reflection objects, where as Java let's you be the middle-man in loading, letting you put whatever you want in as well, although .NET seems to have more framework already laid out for you, if you don't want to learn JBC.
Yeah I'm using ILDASM and it makes it feel like a piece of cake. The long work will be to either map disassembled class dumps words to OpCodes/Builders or actually do some interpreter work (Sadly I don't think I can fully avoid/automate parsing of new written code). Owell, got work on my hands, job+school are time consuming so I might not progress quickly. Will keep this updated when I do.
xXOpkillerXx is offline   Reply With Quote
Old 10-16-2016, 04:39 PM   #16
Soundwave-
Carry your failures proud
FFR Veteran
 
Soundwave-'s Avatar
 
Join Date: Sep 2015
Age: 23
Posts: 644
Default Re: Reflection.Emit

Quote:
Originally Posted by xXOpkillerXx View Post
Yeah I'm using ILDASM and it makes it feel like a piece of cake. The long work will be to either map disassembled class dumps words to OpCodes/Builders or actually do some interpreter work (Sadly I don't think I can fully avoid/automate parsing of new written code). Owell, got work on my hands, job+school are time consuming so I might not progress quickly. Will keep this updated when I do.
Well what exactly is the use case here?

I am of the opinion that parsing code and using expressions will be much easier than any sort of disassembly/reassembly work, but I don't know what exactly you're trying to do so dunno.
__________________
Quote:
Originally Posted by [11:38 PM] Hakulyte
only person who can legit tilt me is like YoshL
Quote:
Originally Posted by スンファンさん
右に3回回らない限り間違います。
Soundwave- is offline   Reply With Quote
Old 10-16-2016, 04:51 PM   #17
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Re: Reflection.Emit

Quote:
Originally Posted by Soundwave- View Post
Well what exactly is the use case here?

I am of the opinion that parsing code and using expressions will be much easier than any sort of disassembly/reassembly work, but I don't know what exactly you're trying to do so dunno.
Parsing code if a big pain in the ass I already tried that sometime this year for another project. You gotta have all the language syntax rules laid out and that is just bs.

Tbh the final goal isn't so clear to me either, but I'm thinking about writing code in idk a textbox in the app, have it compile and run when I want to. Something like that.
xXOpkillerXx is offline   Reply With Quote
Old 10-16-2016, 05:44 PM   #18
Soundwave-
Carry your failures proud
FFR Veteran
 
Soundwave-'s Avatar
 
Join Date: Sep 2015
Age: 23
Posts: 644
Default Re: Reflection.Emit

Quote:
Originally Posted by xXOpkillerXx View Post
Parsing code if a big pain in the ass I already tried that sometime this year for another project. You gotta have all the language syntax rules laid out and that is just bs.

Tbh the final goal isn't so clear to me either, but I'm thinking about writing code in idk a textbox in the app, have it compile and run when I want to. Something like that.
Yeah there's no way around parsing afaik unless you can put it into some form that can be compiled with MSVC.

I believe there's fairly rigorous lexical specifications for C# which definitely simplifies your job in that regard though.
__________________
Quote:
Originally Posted by [11:38 PM] Hakulyte
only person who can legit tilt me is like YoshL
Quote:
Originally Posted by スンファンさん
右に3回回らない限り間違います。
Soundwave- is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 10:18 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution