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

Various amounts of help and answering of questions required

Started by Matrixmage, 28 July 2012 - 06:15 PM
Matrixmage #1
Posted 28 July 2012 - 08:15 PM
Hi, I'm relatively new to using lua, but have done some C# so i know a bit of what I'm doing. I've been writing some programs in notepad ++ for use on my private tekkit server and I've come across a few problems and many questions that need some expert help.

1. what is the difference between fs.copy and cp? also is there a delete version of fs.copy as their is rm to cp?

2. is there a way to suppress Ctrl + R or Ctrl + S like there is with Ctrl + T?

3. is there a way to get a program to "ping" a computer to see what programs are on it?

4. can I have a disk do one thing the first time I put it in a computer and something else (could it leave a tag on a computer the disk goes in and have the next time the disk goes into the computer it checks to see if the tag is there?)

5. is their a way to have a piece of code "spread" over rednet (wireless and wired) such as a standardized piece of code for a certain network?

6. is there a way to have a disk be ignored as the priority boot drive?

As these things would be implemented on a server, I wouldn't be able to have users modify any files to make these things happen.

If I have an more question I'll be sure to post them.

Thanks in advance!
Illmatar #2
Posted 28 July 2012 - 08:25 PM
4. can I have a disk do one thing the first time I put it in a computer and something else (could it leave a tag on a computer the disk goes in and have the next time the disk goes into the computer it checks to see if the tag is there?)
With this I would think that making the disks program create a file (on the disk) to store the ids of computers it has been used on and along with that having it check the file for the ids not exactly sure how to do this but I would think that should work out.
Tiin57 #3
Posted 28 July 2012 - 08:31 PM
1: No. No difference, except that one is a shell program, and the other is part of the fs api and cannot be run from shell.
2: Not as far as I know.
3: Possibly if you want to spend a couple days creating a client/server setup. I might. It could be useful.
4: See 3.
5: See 4.
6: Not really.
As you can tell, I like client/server operations. :)/>/>
MysticT #4
Posted 28 July 2012 - 08:31 PM
1) fs.copy is a function (inside the fs api), cp is a program (actually is an alias to the copy program). To delete file use fs.delete (this is a function like fs.copy).
2) No, they are part of the mod, coded in java, so you can't prevent that.
3) Yes, but you need to have a program in both computers. One to send the message and the other to receive the ping message and send the answer.
4) See first post. There's other options though, but that one is very good.
5) Not sure what you mean. If you want to create a network, there's already some programs to do it (check the program library). If you want to "spread" code over rednet (like a virus or something), you need to have the receiving computers waiting for the code and execute it.
6) No, but you can place a disk drive on top (higher priority) with a blank startup in a floppy.

EDIT: ninja'd :)/>/>
Matrixmage #5
Posted 28 July 2012 - 08:57 PM
4. can I have a disk do one thing the first time I put it in a computer and something else (could it leave a tag on a computer the disk goes in and have the next time the disk goes into the computer it checks to see if the tag is there?)
With this I would think that making the disks program create a file (on the disk) to store the ids of computers it has been used on and along with that having it check the file for the ids not exactly sure how to do this but I would think that should work out.

Could you give an example of the code i would need?

3: Possibly if you want to spend a couple days creating a client/server setup. I might. It could be useful.
4: See 3.
5: See 4.
As you can tell, I like client/server operations. :)/>/>

Sorry I'm pretty new to this, could you explain what you mean by a client/server setup?

3) Yes, but you need to have a program in both computers. One to send the message and the other to receive the ping message and send the answer.

What I meant was if I could have a program (maybe on a disk) check to see all the programs on a computer and possibly implement them in its own code

5) Not sure what you mean. If you want to create a network, there's already some programs to do it (check the program library). If you want to "spread" code over rednet (like a virus or something), you need to have the receiving computers waiting for the code and execute it.

Could I have that work passively, such as it being in the startup code or even hardwired into a part of the os? (the auto-executing part would not be needed)

Thank you all for your help!
MysticT #6
Posted 28 July 2012 - 09:08 PM
3) Yes, but you need to have a program in both computers. One to send the message and the other to receive the ping message and send the answer.

What I meant was if I could have a program (maybe on a disk) check to see all the programs on a computer and possibly implement them in its own code
Oh, that's easier. shell.programs returns a list of all the available programs.
Example:

local programs = shell.programs() -- get the list of programs
-- print them all
for _, s in ipairs(programs) do
  print(s)
end

It only gets the programs on the path (/rom/programs and the current directory). If you need to get every file on the computer it would need some more work, but it's possible.

5) Not sure what you mean. If you want to create a network, there's already some programs to do it (check the program library). If you want to "spread" code over rednet (like a virus or something), you need to have the receiving computers waiting for the code and execute it.

Could I have that work passively, such as it being in the startup code or even hardwired into a part of the os? (the auto-executing part would not be needed)
Yes, you can use the parallel api to run a function on the background. Or you can use my os and run it as a service (background program, started automatically at startup) :)/>/>
Illmatar #7
Posted 28 July 2012 - 10:26 PM
Could you give an example of the code i would need?



-- Functions --

function OpenReadFile() -- Open File for reading --
	computerids = fs.open('ids', 'r')
	if not computerids then
		print("ERROR")
	end
end

function OpenAddFile() -- Open file for adding line --
	computerids = fs.open('ids', 'a')
end

function FindID() -- Searches file for the computers id --
	OpenReadFile()
	first = true
	repeat
		currentLine = computerids.readLine()
		if os.computerID() == tonumber(currentLine) then
			first = false
			NotFirstRun()
		end
		sleep(0.2)
	until currentLine == nil
	if first == true then
		FirstRun()
	end
	computerids.close()
end

function AddID() -- Adds current computers id --
	OpenAddFile()
	AddLine = os.computerID()
	computerids.writeLine(AddLine)
	computerids.close()
end

function	FirstRun() -- What you want to run if it is first time --
	AddID()
end

function NotFirstRun() -- What you want to run if not first time --

end

-- Start Program --

FindID()

That should do the trick for the id checking
Matrixmage #8
Posted 28 July 2012 - 10:44 PM
Could you give an example of the code i would need?



-- Functions --

function OpenReadFile() -- Open File for reading --
	computerids = fs.open('ids', 'r')
	if not computerids then
		print("ERROR")
	end
end

function OpenAddFile() -- Open file for adding line --
	computerids = fs.open('ids', 'a')
end

function FindID() -- Searches file for the computers id --
	OpenReadFile()
	first = true
	repeat
		currentLine = computerids.readLine()
		if os.computerID() == tonumber(currentLine) then
			first = false
			NotFirstRun()
		end
		sleep(0.2)
	until currentLine == nil
	if first == true then
		FirstRun()
	end
	computerids.close()
end

function AddID() -- Adds current computers id --
	OpenAddFile()
	AddLine = os.computerID()
	computerids.writeLine(AddLine)
	computerids.close()
end

function	FirstRun() -- What you want to run if it is first time --
	AddID()
end

function NotFirstRun() -- What you want to run if not first time --

end

-- Start Program --

FindID()

That should do the trick for the id checking

Thanks!
Matrixmage #9
Posted 28 July 2012 - 10:45 PM
5) Not sure what you mean. If you want to create a network, there's already some programs to do it (check the program library). If you want to "spread" code over rednet (like a virus or something), you need to have the receiving computers waiting for the code and execute it.

Could I have that work passively, such as it being in the startup code or even hardwired into a part of the os? (the auto-executing part would not be needed)
Yes, you can use the parallel api to run a function on the background. Or you can use my os and run it as a service (background program, started automatically at startup) :)/>/>

Whats a parallel api and how would I use it? Sorry I'm new to this :D/>/>
MysticT #10
Posted 28 July 2012 - 10:58 PM
Apis are a group of functions writen on a file, that you can load using the os.loadAPI function.
The parallel api has functions that let you run multiple functions at the same time (it's not actaully at the same time, but it looks like). Using it you can run a function to do whatever you want on the background, and a function that runs the shell.
It would be something like:

local function background()
  while true do -- make it run in a loop
    -- your code here
  end
end

parallel.waitForAny(background, function() shell.run("rom/programs/shell") end)
Matrixmage #11
Posted 28 July 2012 - 11:10 PM
Apis are a group of functions writen on a file, that you can load using the os.loadAPI function.
The parallel api has functions that let you run multiple functions at the same time (it's not actaully at the same time, but it looks like). Using it you can run a function to do whatever you want on the background, and a function that runs the shell.
It would be something like:

local function background()
  while true do -- make it run in a loop
	-- your code here
  end
end

parallel.waitForAny(background, function() shell.run("rom/programs/shell") end)

So this code would be the kind I would use for having that work passively?
MysticT #12
Posted 28 July 2012 - 11:18 PM
Yes, you just need add the code you want to run on the background (passively) inside the function. It will start running the function and create a new shell where you can use other programs.
Matrixmage #13
Posted 01 August 2012 - 09:11 PM
Yes, you just need add the code you want to run on the background (passively) inside the function. It will start running the function and create a new shell where you can use other programs.

Thanks for all your help!