Autoupgraderscriptbyfredhuffhines_1.lsl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // :CATEGORY:Upgrade Script
  2. // :NAME:Auto_upgrader
  3. // :AUTHOR:Fred Huffhines
  4. // :CREATED:2010-01-10 05:20:56.000
  5. // :EDITED:2013-09-18 15:38:48
  6. // :ID:65
  7. // :NUM:92
  8. // :REV:1.0
  9. // :WORLD:Second Life
  10. // :DESCRIPTION:
  11. // Auto-upgrader-script-by-fred-huffhines.lsl
  12. // :CODE:
  13. //////////////
  14. // auto-upgrader, by fred huffhines, distributed under GPL license.
  15. // partly based on the self-upgrading scripts from markov brodsky and jippen fadoul.
  16. // the function auto_upgrade() should be added to a version numbered script that you wish
  17. // to give the capability of self-upgrading. see example invocation at end of file.
  18. // this script supports a notation for versions where a 'v' character and a number in the
  19. // form "major.minor" reside at the end of script names, e.g. "grunkle script by ted v8.2".
  20. // when the script is dropped into an object with a different version, the most recent
  21. // version eats any existing one.
  22. integer posn;
  23. auto_upgrade()
  24. {
  25. string self = llGetScriptName(); // the name of this script.
  26. string basename = self; // script name with no version attached.
  27. if (llSubStringIndex(self, " ") >= 0)
  28. {
  29. // minimum script name is 2 characters plus version.
  30. for (posn = llStringLength(self) - 1; (posn >= 2) && (llGetSubString(self, posn, posn) != " "); posn--)
  31. {
  32. // find the space.
  33. }
  34. if (posn < 2) return; // no space found.
  35. string suffix = llGetSubString(self, posn, -1);
  36. // ditch the space character for our numerical check.
  37. string chopped_suffix = llGetSubString(suffix, 1, llStringLength(suffix) - 1);
  38. // strip out a 'v' if there is one.
  39. if (llGetSubString(chopped_suffix, 0, 0) == "v")
  40. chopped_suffix = llGetSubString(chopped_suffix, 1, llStringLength(chopped_suffix) - 1);
  41. // if it's a valid floating point number and is greater than zero, that works for our version.
  42. if ((float)chopped_suffix > 0.0)
  43. basename = llGetSubString(self, 0, -llStringLength(suffix) - 1);
  44. }
  45. // find any scripts that match the basename. they are variants of this script.
  46. for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--)
  47. {
  48. string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
  49. // remove scripts with same name (except myself, of course).
  50. if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) )
  51. {
  52. llRemoveInventory(curr_script);
  53. }
  54. }
  55. }
  56. //////////////
  57. // example usage of the upgrader:
  58. default
  59. {
  60. state_entry()
  61. {
  62. auto_upgrade();
  63. }
  64. } // end