Advertisement
skypop

CC Peripheral Manager

Aug 9th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.81 KB | None | 0 0
  1. --------------------------------------------------------
  2. -- Peripheral manager
  3. -- by SukaiPoppuGo
  4. -- Modpack: Luapolis 1.3 / ComputerCraft
  5. -- Generation du code d'un API pour controuner les bugs
  6. -- lies a certains peripheriques.
  7. --------------------------------------------------------
  8. -- Params:
  9. -- sFileName : chemin/nom du fichier
  10. -- sSide : nom du cote par lequel le peripherique est
  11. --         attache, ou bien son identifiant dans le network
  12. local sFileName, sSide = ...
  13.  
  14. --------------------------------------------------------
  15. print("Verification des parametres")
  16. if not sFileName or not sSide then
  17.     printError("Usage: <string:fileName> <string:side>")
  18.     return
  19. elseif fs.exists(sFileName) then
  20.     local confirm
  21.     repeat
  22.         printError("Fichier:", sFileName, "deja existant")
  23.         print("Ecraser ? Y/N")
  24.         confirm = string.lower( read() )
  25.         if confirm ~= "y" and confirm ~= "n" then
  26.             printError("Reponse invalide")
  27.             print(string.format([[Veuillez repondre :
  28.   N (No) pour annuler
  29.   Y (Yes) pour ecraser le fichier :
  30. %s]], sFileName))
  31.         end
  32.     until confirm == "y" or confirm == "n"
  33.     if confirm == "n" then
  34.         return
  35.     end
  36. end
  37.  
  38. if not peripheral.isPresent(sSide) then
  39.     printError("Peripherique introuvable:", sSide)
  40.     return
  41. end
  42.  
  43. --------------------------------------------------------
  44. print("Examen du peripherique")
  45. local sTtype = peripheral.getType(sSide)
  46. local tMethodsList = peripheral.getMethods(sSide)
  47.  
  48. --------------------------------------------------------
  49. print("Preparation du fichier")
  50. local file = fs.open(sFileName, "w")
  51.  
  52. file.write(string.format([[-- Peripheral manager: (%s) %s
  53. local sSide = "%s"
  54.  
  55. local function _rawCall(sMethod, ...)
  56.     return peripheral.call(sSide, sMethod, ...)
  57. end
  58. ]],
  59. sSide, sTtype, sSide))
  60. --END file.write
  61.  
  62. print("Copie des methodes")
  63. for i,sMethod in pairs(tMethodsList) do
  64.     print(" ", i.."/"..#tMethodsList, sMethod)
  65.     file.write(string.format([[
  66. function %s(...)
  67.     return _rawCall("%s", ...)
  68. end
  69. ]], sMethod, sMethod))
  70.     --END file.write
  71. end
  72.  
  73. --------------------------------------------------------
  74. print("Sauvegarde:", sFileName)
  75. file.close()
  76. local ext = settings.get("edit.default_extension", "" )
  77. if not fs.exists(sFileName) then
  78.     printError("Echec: Le fichier n'a pu etre enregistre:", sFileName)
  79.     return
  80. elseif not os.loadAPI(sFileName) then
  81.     printError("Bug:", sFileName, "echoue a fonctionner comme un API")
  82.     return
  83. elseif string.sub(sFileName,-4) == ".lua" then
  84.     os.unloadAPI(fs.getName(string.sub(sFileName,1,-5)))
  85. elseif string.sub(sFileName,-1*(string.len(ext)+1)) == "."..ext then
  86.     os.unloadAPI(fs.getName(string.sub(sFileName,1,-1*(string.len(ext)+1))))
  87. else
  88.     os.unloadAPI(fs.getName(sFileName))
  89. end
  90. print("Succes")
  91. print("Vous pourrez employer ce peripherique via l'API:")
  92. print(string.format([[os.loadAPI("%s")]], sFileName))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement