as_restore.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_restore.h
  25. //
  26. // Functions for saving and restoring module bytecode
  27. // asCRestore was originally written by Dennis Bollyn, [email protected]
  28. // TODO: This should be split in two, so that an application that doesn't compile any
  29. // code but only loads precompiled code can link with only the bytecode loader
  30. #ifndef AS_RESTORE_H
  31. #define AS_RESTORE_H
  32. #include "as_scriptengine.h"
  33. #include "as_context.h"
  34. #include "as_map.h"
  35. BEGIN_AS_NAMESPACE
  36. class asCRestore
  37. {
  38. public:
  39. asCRestore(asCModule *module, asIBinaryStream *stream, asCScriptEngine *engine);
  40. int Save();
  41. int Restore();
  42. protected:
  43. asCModule *module;
  44. asIBinaryStream *stream;
  45. asCScriptEngine *engine;
  46. bool error;
  47. void WriteData(const void *data, asUINT size);
  48. void ReadData(void *data, asUINT size);
  49. void WriteString(asCString *str);
  50. void WriteFunction(asCScriptFunction *func);
  51. void WriteFunctionSignature(asCScriptFunction *func);
  52. void WriteGlobalProperty(asCGlobalProperty *prop);
  53. void WriteObjectProperty(asCObjectProperty *prop);
  54. void WriteDataType(const asCDataType *dt);
  55. void WriteObjectType(asCObjectType *ot);
  56. void WriteObjectTypeDeclaration(asCObjectType *ot, int phase);
  57. void WriteByteCode(asDWORD *bc, int length);
  58. void WriteEncodedUInt(asUINT i);
  59. void ReadString(asCString *str);
  60. asCScriptFunction *ReadFunction(bool addToModule = true, bool addToEngine = true, bool addToGC = true);
  61. void ReadFunctionSignature(asCScriptFunction *func);
  62. void ReadGlobalProperty();
  63. void ReadObjectProperty(asCObjectType *ot);
  64. void ReadDataType(asCDataType *dt);
  65. asCObjectType *ReadObjectType();
  66. void ReadObjectTypeDeclaration(asCObjectType *ot, int phase);
  67. void ReadByteCode(asDWORD *bc, int length);
  68. asUINT ReadEncodedUInt();
  69. // Helper functions for storing variable data
  70. int FindObjectTypeIdx(asCObjectType*);
  71. asCObjectType *FindObjectType(int idx);
  72. int FindTypeIdIdx(int typeId);
  73. int FindTypeId(int idx);
  74. int FindFunctionIndex(asCScriptFunction *func);
  75. asCScriptFunction *FindFunction(int idx);
  76. int FindGlobalPropPtrIndex(void *);
  77. int FindStringConstantIndex(int id);
  78. int FindObjectPropIndex(short offset, int typeId);
  79. short FindObjectPropOffset(asWORD index);
  80. // Intermediate data used for storing that which isn't constant, function id's, pointers, etc
  81. void WriteUsedTypeIds();
  82. void WriteUsedFunctions();
  83. void WriteUsedGlobalProps();
  84. void WriteUsedStringConstants();
  85. void WriteUsedObjectProps();
  86. void ReadUsedTypeIds();
  87. void ReadUsedFunctions();
  88. void ReadUsedGlobalProps();
  89. void ReadUsedStringConstants();
  90. void ReadUsedObjectProps();
  91. // After loading, each function needs to be translated to update pointers, function ids, etc
  92. void TranslateFunction(asCScriptFunction *func);
  93. // Temporary storage for persisting variable data
  94. asCArray<int> usedTypeIds;
  95. asCArray<asCObjectType*> usedTypes;
  96. asCArray<asCScriptFunction*> usedFunctions;
  97. asCArray<void*> usedGlobalProperties;
  98. asCArray<int> usedStringConstants;
  99. asCMap<int, int> stringIdToIndexMap;
  100. asCArray<asCScriptFunction*> savedFunctions;
  101. asCArray<asCDataType> savedDataTypes;
  102. asCArray<asCString> savedStrings;
  103. asCMap<asCStringPointer, int> stringToIdMap;
  104. struct SObjProp
  105. {
  106. asCObjectType *objType;
  107. int offset;
  108. };
  109. asCArray<SObjProp> usedObjectProperties;
  110. struct SObjChangeSize
  111. {
  112. asCObjectType *objType;
  113. asUINT oldSize;
  114. };
  115. asCArray<SObjChangeSize> oldObjectSizes;
  116. asCMap<void*,bool> existingShared;
  117. asCMap<asCScriptFunction*,bool> dontTranslate;
  118. };
  119. END_AS_NAMESPACE
  120. #endif //AS_RESTORE_H