gcsx_script.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* GCSx
  2. ** SCRIPT.H
  3. **
  4. ** Script support
  5. ** Doesn't include any editor-only functionality
  6. */
  7. /*****************************************************************************
  8. ** Copyright (C) 2003-2006 Janson
  9. **
  10. ** This program is free software; you can redistribute it and/or modify
  11. ** it under the terms of the GNU General Public License as published by
  12. ** the Free Software Foundation; either version 2 of the License, or
  13. ** (at your option) any later version.
  14. **
  15. ** This program is distributed in the hope that it will be useful,
  16. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ** GNU General Public License for more details.
  19. **
  20. ** You should have received a copy of the GNU General Public License
  21. ** along with this program; if not, write to the Free Software
  22. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  23. *****************************************************************************/
  24. #ifndef __GCSx_SCRIPT_H_
  25. #define __GCSx_SCRIPT_H_
  26. class Script : virtual public LoadOnly {
  27. protected:
  28. // Is the script cached to our file and not in memory?
  29. int cached;
  30. FileRead* cacheFile;
  31. int lockCount;
  32. // Name of script, where it's located
  33. std::string name;
  34. std::string nameL;
  35. class World* world; // NULL if not yet added to a world
  36. // Numeric ID- computer-generated- unique to world- nonzero
  37. int id;
  38. // Notes are just scripts that aren't compiled
  39. public:
  40. enum ScriptType {
  41. SCRIPT_CODE = 0,
  42. SCRIPT_NOTE = 1,
  43. SCRIPT_LIBRARY = 2,
  44. };
  45. protected:
  46. ScriptType scriptType;
  47. // Default sprite representation (can be changed individually)
  48. // Only one (animgroup or tileset) can be set, but both can be NULL
  49. // Script never locks these
  50. class AnimGroup* defaultAnimgroup;
  51. class TileSet* defaultTileset;
  52. int defaultId; // (which anim or tile to use)
  53. // Status of info on disk and in memory may differ
  54. public:
  55. enum ScriptStatus {
  56. CODE_UNCOMPILED = 0,
  57. CODE_PARSED,
  58. CODE_COMPILED,
  59. CODE_LINKED // Not valid for disk status
  60. };
  61. protected:
  62. ScriptStatus diskStatus;
  63. int diskSource; // Is disk source present?
  64. ScriptStatus memStatus;
  65. int compileWarnings;
  66. int compileErrors;
  67. // Not cached- part of header, as needed to link other scripts too
  68. FunctionMap* functions; // Matches memStatus
  69. // @TODO: security checks/tainting? (do on a global level for any
  70. // unique filename+timestamp)
  71. // Data (cacheable) uncompiled and compiled
  72. std::list<std::string> source; // May not be present during gameplay
  73. Uint32* bytecode; // Matches memStatus
  74. std::list<struct LinkEntry>* links; // Present whenever bytecode is
  75. void doLink();
  76. public:
  77. // Id must be unique; use ID of 0 if we're going to load anyways
  78. // World and ID may be NULL/0 if in process of creating
  79. Script(class World* myWorld, ScriptType type = SCRIPT_CODE, int myId = 0); // Starts with default settings
  80. virtual ~Script();
  81. // Must LOCK first
  82. // These only compile as needed
  83. // Errors may result
  84. virtual void parseFuncs();
  85. virtual void compile(); // Includes parse if not done
  86. // ASSERTS compilation and no errors; safe to call even if linked
  87. void collectGlobalLinks(World::GlobalMap* globalLinks, int* globalLinksCount);
  88. // ASSERTS compilation and no errors; safe to call even if linked
  89. // Normally called either pre-game, or by the entity using the script
  90. virtual void link();
  91. int numWarnings() const { return compileWarnings; }
  92. int numErrors() const { return compileErrors; }
  93. ScriptStatus status() const { return std::max(diskStatus, memStatus); }
  94. // Accessors
  95. const class World* getWorld() const { return world; }
  96. class World* getWorld() { return world; }
  97. int getId() const { return id; }
  98. int getBlockType() const { return WorldFileLoad::BLOCKTYPE_SCRIPT; }
  99. const std::string& getName() const { return name; }
  100. const std::string& getNameL() const { return nameL; }
  101. ScriptType getType() const { return scriptType; }
  102. class AnimGroup* getDefaultAnimgroup() { return defaultAnimgroup; }
  103. class TileSet* getDefaultTileset() { return defaultTileset; }
  104. int getDefaultId() const { return defaultId; }
  105. // Must be locked, compiled, and linked!
  106. Uint32* getCode();
  107. // File access
  108. void loadHeader(FileRead* file) throw_File;
  109. void loadContent(FileRead* file);
  110. int isContentCached() const;
  111. void cacheLoad() throw_File;
  112. int markLock() throw_File;
  113. int markUnlock();
  114. int isLocked() const;
  115. // Any content-accessor functions- remember to call markLock first!
  116. };
  117. #endif