Jump to content




Apple API

api

  • You cannot reply to this topic
26 replies to this topic

#1 applesauce10189

  • Members
  • 162 posts

Posted 03 May 2014 - 09:32 PM

I'm new to coding and thought I'd make an API dedicated to making coding a little easier. So far it just has a few functions based off of repetitive things I've been putting into code. If you can think of a function to add tell me.

Reset

Usage: apple.reset()

Clears the screen and if it's colored it changes the colors to default. Also sets cursor to top left.


Receive

Usage: if apple.receive(message, ID) then...

Waits for specified message then returns true. Can optionally wait for ID to send that message.



http API Check

Usage: if apple.httpCheck() then...

Will return true or false based off of whether or not the HTTP API is enabled.


File Merge

Usage: apple.fMerge(file1, file2)

Takes the contents of file1 and file2 then combines them and puts them back in file1.


pastebin get AqUvfNXi apple


Edited by applesauce10189, 05 May 2014 - 07:56 AM.


#2 logsys

  • Members
  • 171 posts

Posted 03 May 2014 - 09:49 PM

Can you explain what this does?

#3 applesauce10189

  • Members
  • 162 posts

Posted 03 May 2014 - 10:45 PM

View Postlogsys, on 03 May 2014 - 09:49 PM, said:

Can you explain what this does?
It's an API I'm making just for the sake of making coding relatively easier. For now it just has things that have come in use for things I'm making in my single player world which is why I want suggestions. The individual functions are explained above.

#4 Agoldfish

  • Members
  • 451 posts
  • LocationSome Fish Bowl in Ohio.

Posted 03 May 2014 - 11:42 PM

Add a check for http and color.

#5 applesauce10189

  • Members
  • 162 posts

Posted 04 May 2014 - 01:29 AM

View PostAgoldfish, on 03 May 2014 - 11:42 PM, said:

Add a check for http and color.
It doesn't use the http API, the end is the pastebin code to get the program. It already checks for color when resetting to default colors.

EDIT: Now that I know what you originally meant, just thought I'd let you know term.isColor() already checks whether or not it's color.

Edited by applesauce10189, 04 May 2014 - 09:08 PM.


#6 Agoldfish

  • Members
  • 451 posts
  • LocationSome Fish Bowl in Ohio.

Posted 04 May 2014 - 01:56 AM

View Postapplesauce10189, on 04 May 2014 - 01:29 AM, said:

View PostAgoldfish, on 03 May 2014 - 11:42 PM, said:

Add a check for http and color.
It doesn't use the http API, the end is the pastebin code to get the program. It already checks for color when resetting to default colors.
I meant add a function in your API to check for http.

#7 applesauce10189

  • Members
  • 162 posts

Posted 04 May 2014 - 02:59 AM

View PostAgoldfish, on 03 May 2014 - 11:42 PM, said:

I meant add a function in your API to check for http.
Okay that makes more sense,

#8 Lignum

  • Members
  • 558 posts

Posted 04 May 2014 - 08:13 PM

I can imagine that this would come in handy when it has more functions. However, I don't think I'd want to ship my program with an API just to make my life easier. So perhaps you could write a tool that merges a program with your API? Speaking of which, you could make a file merging function.

#9 applesauce10189

  • Members
  • 162 posts

Posted 04 May 2014 - 08:46 PM

API updated. Added http api check function.

#10 Link149

  • Members
  • 46 posts
  • LocationQuebec, Canada

Posted 04 May 2014 - 08:48 PM

Quote

Receive
Usage: if apple.receive(message) then...
Waits for specified message then returns true.

Advanced Receive
Usage: if apple.aReceive(id, message) then...
Waits for specified ID to send specified message.

Why don't you merge these functions in a single one instead ?
You could check whether the type of the first argument is a string, if so then
it is the message. Otherwise, if it is a number then it is the id.

function apple.receive(nId, sMessage)
  if type(nId) == "number" and type(sMessage) == "string" then
	--# Pauses until it receives a specified message from a specified id.
  elseif type(nId) == "string" then
    --# Pauses until it receives a specified message.
  end
end

Another solution would be to check if the first argument is nil.

function apple.receive(nId, sMessage)
  if nId == nil then
	--# Wait for the specified message
  elseif type(nId) == "number" and type(sMessage) == "string" then
	--# Wait for the specified message from the specified id.
  end
end

Edited by Link149, 04 May 2014 - 08:49 PM.


#11 applesauce10189

  • Members
  • 162 posts

Posted 04 May 2014 - 08:52 PM

View PostLignum, on 04 May 2014 - 08:13 PM, said:

I can imagine that this would come in handy when it has more functions. However, I don't think I'd want to ship my program with an API just to make my life easier. So perhaps you could write a tool that merges a program with your API? Speaking of which, you could make a file merging function.
I'm still not 100% sure but one computercraft wiki look up later, it appears fs.combine() already does this, if I'm wrong please correct me.

View PostLink149, on 04 May 2014 - 08:48 PM, said:

Quote

Receive
Usage: if apple.receive(message) then...
Waits for specified message then returns true.

Advanced Receive
Usage: if apple.aReceive(id, message) then...
Waits for specified ID to send specified message.

Why don't you merge these functions in a single one instead ?
You could check whether the type of the first argument is a string, if so then
it is the message. Otherwise, if it is a number then it is the id.

function apple.receive(nId, sMessage)
  if type(nId) == "number" and type(sMessage) == "string" then
	--# Pauses until it receives a specified message from a specified id.
  elseif type(nId) == "string" then
	--# Pauses until it receives a specified message.
  end
end

Another solution would be to check if the first argument is nil.

function apple.receive(nId, sMessage)
  if nId == nil then
	--# Wait for the specified message
  elseif type(nId) == "number" and type(sMessage) == "string" then
	--# Wait for the specified message from the specified id.
  end
end
That, is actually extremely useful thank you. I'll get started on that right now.

EDIT: Updated program.

Edited by applesauce10189, 04 May 2014 - 09:04 PM.


#12 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 04 May 2014 - 09:00 PM

View Postapplesauce10189, on 04 May 2014 - 08:52 PM, said:

I'm still not 100% sure but one computercraft wiki look up later, it appears fs.combine() already does this, if I'm wrong please correct me.

fs.combine takes two strings (paths) and adds (concatenates) them the correct way:

print( fs.combine( "test\\//" , "\\\\my folder/file.txt/////" ) ) -->> 'test/my folder/file.txt'


#13 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 04 May 2014 - 09:04 PM

How can you know for sure which computer a message comes from, when you can spoof your id?

#14 applesauce10189

  • Members
  • 162 posts

Posted 04 May 2014 - 10:00 PM

View Postawsmazinggenius, on 04 May 2014 - 09:04 PM, said:

How can you know for sure which computer a message comes from, when you can spoof your id?
I don't quite think you can fake an ID, but even if you can, this isn't really a security program, that function simply checks if it received a certain message, from a certain ID if you choose to add the ID it should come from.

View PostMKlegoman357, on 04 May 2014 - 09:00 PM, said:

View Postapplesauce10189, on 04 May 2014 - 08:52 PM, said:

I'm still not 100% sure but one computercraft wiki look up later, it appears fs.combine() already does this, if I'm wrong please correct me.

fs.combine takes two strings (paths) and adds (concatenates) them the correct way:

print( fs.combine( "test\\//" , "\\\\my folder/file.txt/////" ) ) -->> 'test/my folder/file.txt'

View PostMKlegoman357, on 04 May 2014 - 09:00 PM, said:

View Postapplesauce10189, on 04 May 2014 - 08:52 PM, said:

I'm still not 100% sure but one computercraft wiki look up later, it appears fs.combine() already does this, if I'm wrong please correct me.

fs.combine takes two strings (paths) and adds (concatenates) them the correct way:

print( fs.combine( "test\\//" , "\\\\my folder/file.txt/////" ) ) -->> 'test/my folder/file.txt'
Thank you for telling me. I'll get to work on that file merge that was suggested then.

#15 LDShadowLord

  • Members
  • 115 posts

Posted 04 May 2014 - 11:23 PM

You can spoof an ID, quite easily. (I think, unless 1.6 removed that?) But even so, adding the necessary security to check would be extensive and unnecessary on both the API and the Client.

#16 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 04 May 2014 - 11:33 PM

CC 1.6 didn't change that and it's unlikely it'll ever change.

The idea is that what the RedNet API calls an "ID" is actually a modem channel, and users can send / receive using pretty much whatever channel they like by accessing the modem API directly. This means that anyone on your wired network, or in range of your wireless network, can intercept and read a copy of any messages they like (regardless as to where you wanted to send them), and they can send messages out as though they owned a computer with any ID they like (regardless as to whether the computers they have access to have those IDs).

#17 applesauce10189

  • Members
  • 162 posts

Posted 05 May 2014 - 07:58 AM

Updated program. Wasn't 100% sure how to put in Lignum's file merge idea but here's what I came up with! Any more ideas would be greatly appreciated.

EDIT: Well. I say program. It's an API.

EDIT#2: Y'know, now that I think about it, the HTTP API check is fairy useless because if you have an API from the internet then you more than likely used the pastebin function from HTTP API to get it. Unless you were too lazy to go into the config and decided to copy/paste the pastebin instead...

EDIT#3: Actually, I would assume typing 'apis' into the shell would tell you if HTTP API is enabled or not, considering if it is, it'll show up, if it isn't, it won't be listed. That actually leads me to a question, why is HTTP API default turned off?

Edited by applesauce10189, 05 May 2014 - 08:58 AM.


#18 viluon

  • Members
  • 183 posts
  • LocationCzech Republic

Posted 05 May 2014 - 09:00 AM

I understand that this could be useful to you. However, I don't see any point in posting it here as this is fairly simple to do yourself and it doesn't have any real complex functions...

#19 Lignum

  • Members
  • 558 posts

Posted 05 May 2014 - 02:19 PM

View Postapplesauce10189, on 05 May 2014 - 07:58 AM, said:

That actually leads me to a question, why is HTTP API default turned off?
Since ComputerCraft 1.63 it's on by default, however there is a whitelist for accessible domains.

#20 applesauce10189

  • Members
  • 162 posts

Posted 05 May 2014 - 10:29 PM

View Postviluon, on 05 May 2014 - 09:00 AM, said:

I understand that this could be useful to you. However, I don't see any point in posting it here as this is fairly simple to do yourself and it doesn't have any real complex functions...
Well that's why it's here, I can't think of much to put in it so I want suggestions. Suggest something complex and I'll try my best to put it in.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users