This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
sirdabalot's profile picture

Strings to functions.

Started by sirdabalot, 03 October 2012 - 03:50 PM
sirdabalot #1
Posted 03 October 2012 - 05:50 PM
Is this possible? I have looked around and the closest I could find is by using string.dump somehow? I always imagined that it would be more along the lines of this:


string = "write("hello")"
run(string)

Can anyone help me achieve something like this?
Orwell #2
Posted 03 October 2012 - 05:55 PM

str = "write('hello')"
loadstring(str)

PS: You can't call a variable 'string', as that's the name of a metatable allready.
PPS: You did:

"write("hello")"
But you can't use a pair of double quotes inside another pair of double quotes for the simple reason that it doesn't know where the string starts or ends. :(/>/>
sirdabalot #3
Posted 03 October 2012 - 05:59 PM

str = "write('hello')"
loadstring(str)

PS: You can't call a variable 'string', as that's the name of a metatable allready.
PPS: You did:

"write("hello")"
But you can't use a pair of double quotes inside another pair of double quotes for the simple reason that it doesn't know where the string starts or ends. :(/>/>

I know all that it was just dumbed down.
jag #4
Posted 03 October 2012 - 06:27 PM
Well you could do a combination of
  • make a temporary file
  • store the code you want to run in it
  • run the code with shell.run()
  • delete the file
That's the easiest way I can think of
sirdabalot #5
Posted 03 October 2012 - 06:31 PM
Well you could do a combination of
  • make a temporary file
  • store the code you want to run in it
  • run the code with shell.run()
  • delete the file
That's the easiest way I can think of

I'll stick with that for now, then. Thanks! :(/>/>
Cloudy #6
Posted 03 October 2012 - 06:45 PM
loadstring works - why not use that? Storing it to disk before running is just stupid.
sirdabalot #7
Posted 03 October 2012 - 07:24 PM
loadstring works - why not use that? Storing it to disk before running is just stupid.

Because I never knew of it, could you link me to a tutorial, please, I can't seem to find one.
hego555 #8
Posted 04 October 2012 - 07:08 AM
Wow I never knew of loadstring.
Thanks, this may be off topic but… what is the C# equivalent to loadstring?
faubiguy #9
Posted 04 October 2012 - 07:19 AM
Wow I never knew of loadstring.
Thanks, this may be off topic but… what is the C# equivalent to loadstring?
I don't think that there is an equivalent to loadstring in C#. C# is a compiled language ,so the compiler has to know what code to use at compile-time, unlike interpreted languages such as lua.
Minalien #10
Posted 04 October 2012 - 07:21 AM
Wow I never knew of loadstring.
Thanks, this may be off topic but… what is the C# equivalent to loadstring?
It can be done, but not quite as easily as loadstring(). Basically, you need to use the CLR compiler on the user's machine to compile the code, and then load that assembly and execute a specific command in there through Reflection.

Short answer: It's possible, but definitely not something to be undertaken by somebody who isn't intimitely familiar with C# and how the .NET Framework operates.
hego555 #11
Posted 04 October 2012 - 07:25 AM
Wow I never knew of loadstring.
Thanks, this may be off topic but… what is the C# equivalent to loadstring?
It can be done, but not quite as easily as loadstring(). Basically, you need to use the CLR compiler on the user's machine to compile the code, and then load that assembly and execute a specific command in there through Reflection.

Short answer: It's possible, but definitely not something to be undertaken by somebody who isn't intimitely familiar with C# and how the .NET Framework operates.


Frankly there has been something I always wanted to do but never understood, I think you can help.

If you see those RAT's(Remote Administration Tools) they have builders, you give them settings and it compiles the program for you… I dont indeed to be malicious, but I am curious on how it works, please do message me if you can help.
Minalien #12
Posted 04 October 2012 - 07:29 AM
The best thing you can do is to get familiar with the command-line compiler for C# (the specific binary is csc), and how it operates. Then, get familiar with File I/O, and finally with Reflection – after you're comfortable with all of those, it shouldn't be very difficult to work out something that would work well.
hego555 #13
Posted 04 October 2012 - 07:34 AM
The best thing you can do is to get familiar with the command-line compiler for C# (the specific binary is csc), and how it operates. Then, get familiar with File I/O, and finally with Reflection – after you're comfortable with all of those, it shouldn't be very difficult to work out something that would work well.

So wait does this happen?

Code saved to file
CSC is compiler, you give CSC file and it compiles it…

but what is reflection?
Minalien #14
Posted 04 October 2012 - 07:43 AM
Reflection is a way to search through metadata contained within .NET assemblies (DLLs & EXEs that use the .NET Framework) to locate and execute functions, instantiate classes, etc. It's a really powerful tool, and I'd say it's worth learning just for the sake of knowing it, but it could definitely help you accomplish your goal.

Edit:
This article on CodeProject may be able to give you a head start: http://www.codeproject.com/Articles/113169/C-As-A-Scripting-Language-In-Your-NET-Applications
sirdabalot #15
Posted 04 October 2012 - 09:07 AM
Reflection is a way to search through metadata contained within .NET assemblies (DLLs & EXEs that use the .NET Framework) to locate and execute functions, instantiate classes, etc. It's a really powerful tool, and I'd say it's worth learning just for the sake of knowing it, but it could definitely help you accomplish your goal.

Edit:
This article on CodeProject may be able to give you a head start: http://www.codeproje...ET-Applications

I'll have a look.
sirdabalot #16
Posted 04 October 2012 - 10:56 AM
Hold on, I just found the syntax for the loadstring and this isn't working:


rednet.open("back")
event, id, msg, distance = os.pullEvent("rednet_message")
loadstring(msg)

Something tells me I completely missed the point. :(/>/>
Cloudy #17
Posted 04 October 2012 - 11:00 AM
The result of loadstring is a function - so do:
local foo = loadstring(blah)
foo()
sirdabalot #18
Posted 04 October 2012 - 11:08 AM
The result of loadstring is a function - so do:
local foo = loadstring(blah)
foo()

Oh, I hoped it would be that simple, thanks Cloudy, you've really helped me out. :(/>/>
ChaddJackson12 #19
Posted 05 October 2012 - 02:33 AM
Also, you could do something like this…

function helloThere() -- Function name can pretty much be anything, just put () after it.
   write("Hi There!") -- You can have a function do pretty much anything, as well...
   write("Hi There!") -- Even multiple things. -- Just make sure to put an "end" after the function.
end

To call a function you do something like this.


functionname() -- The name of the function goes here, with the ()'s after it.

Or something like this

print("Hi!")
write("> ")
local input = read()
if input == "Hi" then
   functionName()
else
   functionName2()
end

But you don't want a function to print or write one thing. So do this.


hello = "Hey There!"
write(hello)