sfile.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <precomp.h>
  2. #include "SFile.h"
  3. #include <api/script/script.h>
  4. #include <api/script/scriptmgr.h>
  5. #include <api/script/objecttable.h>
  6. // {836F8B2E-E0D1-4db4-937F-0D0A04C8DCD1}
  7. static const GUID fileGuid =
  8. { 0x836f8b2e, 0xe0d1, 0x4db4, { 0x93, 0x7f, 0xd, 0xa, 0x4, 0xc8, 0xdc, 0xd1 } };
  9. fileScriptController _fileController;
  10. fileScriptController *fileController=&_fileController;
  11. // -- Functions table -------------------------------------
  12. function_descriptor_struct fileScriptController::exportedFunction[] = {
  13. {L"load", 1, (void*)SFile::script_vcpu_load },
  14. {L"getSize", 0, (void*)SFile::script_vcpu_getSize },
  15. {L"exists", 0, (void*)SFile::script_vcpu_exists },
  16. };
  17. // --------------------------------------------------------
  18. const wchar_t *fileScriptController::getClassName() {
  19. return L"File";
  20. }
  21. const wchar_t *fileScriptController::getAncestorClassName() {
  22. return L"Object";
  23. }
  24. ScriptObjectController *fileScriptController::getAncestorController() { return rootScriptObjectController; }
  25. ScriptObject *fileScriptController::instantiate() {
  26. SFile *xd = new SFile;
  27. ASSERT(xd != NULL);
  28. return xd->getScriptObject();
  29. }
  30. void fileScriptController::destroy(ScriptObject *o) {
  31. SFile *xd = static_cast<SFile *>(o->vcpu_getInterface(fileGuid));
  32. ASSERT(xd != NULL);
  33. delete xd;
  34. }
  35. void *fileScriptController::encapsulate(ScriptObject *o) {
  36. return NULL; // no encapsulation for files for now
  37. }
  38. void fileScriptController::deencapsulate(void *o) {
  39. }
  40. int fileScriptController::getNumFunctions() {
  41. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  42. }
  43. const function_descriptor_struct *fileScriptController::getExportedFunctions() {
  44. return exportedFunction;
  45. }
  46. GUID fileScriptController::getClassGuid() {
  47. return fileGuid;
  48. }
  49. SFile::SFile() {
  50. getScriptObject()->vcpu_setInterface(fileGuid, (void *)static_cast<SFile *>(this));
  51. getScriptObject()->vcpu_setClassName(L"File");
  52. getScriptObject()->vcpu_setController(fileController);
  53. filename = NULL;
  54. }
  55. SFile::~SFile() {
  56. //if (bmp) delete bmp;
  57. }
  58. void SFile::loadfile(const wchar_t *b)
  59. {
  60. filename = b;
  61. }
  62. // VCPU
  63. scriptVar SFile::script_vcpu_load(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar fn) {
  64. SCRIPT_FUNCTION_INIT;
  65. ASSERT(fn.type == SCRIPT_STRING);
  66. SFile *m = static_cast<SFile *>(o->vcpu_getInterface(fileGuid));
  67. if (m) m->loadfile(fn.data.sdata);
  68. RETURN_SCRIPT_VOID;
  69. }
  70. scriptVar SFile::script_vcpu_getSize(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  71. SCRIPT_FUNCTION_INIT;
  72. SFile *m = static_cast<SFile *>(o->vcpu_getInterface(fileGuid));
  73. if (m)
  74. {
  75. OSFILETYPE in = WFOPEN(m->filename, WF_READONLY_BINARY);
  76. if (in == OPEN_FAILED) RETURN_SCRIPT_ZERO;
  77. int size = (int)FGETSIZE(in);
  78. FCLOSE(in);
  79. return MAKE_SCRIPT_INT(size);
  80. }
  81. RETURN_SCRIPT_ZERO;
  82. }
  83. scriptVar SFile::script_vcpu_exists(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  84. SCRIPT_FUNCTION_INIT;
  85. SFile *m = static_cast<SFile *>(o->vcpu_getInterface(fileGuid));
  86. if (m)
  87. {
  88. OSFILETYPE in = WFOPEN(m->filename, WF_READONLY_BINARY);
  89. if (in == OPEN_FAILED) return MAKE_SCRIPT_BOOLEAN(0);
  90. FCLOSE(in);
  91. return MAKE_SCRIPT_BOOLEAN(1);
  92. }
  93. return MAKE_SCRIPT_BOOLEAN(0);
  94. }