IOAPI.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../AngelScript/APITemplates.h"
  24. #include "../IO/Compression.h"
  25. #include "../IO/FileSystem.h"
  26. #include "../IO/NamedPipe.h"
  27. #include "../IO/PackageFile.h"
  28. namespace Urho3D
  29. {
  30. void FakeAddRef(void* ptr)
  31. {
  32. }
  33. void FakeReleaseRef(void* ptr)
  34. {
  35. }
  36. static Log* GetLog()
  37. {
  38. return GetScriptContext()->GetSubsystem<Log>();
  39. }
  40. #ifdef URHO3D_LOGGING
  41. static void Print(const String& value, bool error)
  42. {
  43. Log::WriteRaw(value + "\n", error);
  44. }
  45. static void Print(int value, bool error)
  46. {
  47. Log::WriteRaw(String(value) + "\n", error);
  48. }
  49. static void Print(long long value, bool error)
  50. {
  51. Log::WriteRaw(String(value) + "\n", error);
  52. }
  53. static void Print(unsigned value, bool error)
  54. {
  55. Log::WriteRaw(String(value) + "\n", error);
  56. }
  57. static void Print(unsigned long long value, bool error)
  58. {
  59. Log::WriteRaw(String(value) + "\n", error);
  60. }
  61. static void Print(float value, bool error)
  62. {
  63. Log::WriteRaw(String(value) + "\n", error);
  64. }
  65. static void Print(bool value, bool error)
  66. {
  67. Log::WriteRaw(String(value) + "\n", error);
  68. }
  69. static void Print(const Variant& value, bool error)
  70. {
  71. Log::WriteRaw(value.ToString() + "\n", error);
  72. }
  73. static void PrintCallStack(bool error)
  74. {
  75. asIScriptContext* context = asGetActiveContext();
  76. if (context)
  77. Log::WriteRaw(Script::GetCallStack(context), error);
  78. }
  79. static void LogWrite(const String& str, bool error, Log* ptr)
  80. {
  81. Log::WriteRaw(str + "\n", error);
  82. }
  83. static void LogTrace(const String& str, Log* ptr)
  84. {
  85. Log::Write(LOG_TRACE, str);
  86. }
  87. static void LogDebug(const String& str, Log* ptr)
  88. {
  89. Log::Write(LOG_DEBUG, str);
  90. }
  91. static void LogInfo(const String& str, Log* ptr)
  92. {
  93. Log::Write(LOG_INFO, str);
  94. }
  95. static void LogWarning(const String& str, Log* ptr)
  96. {
  97. Log::Write(LOG_WARNING, str);
  98. }
  99. static void LogError(const String& str, Log* ptr)
  100. {
  101. Log::Write(LOG_ERROR, str);
  102. }
  103. #else
  104. static void Print(const String& value, bool error) { }
  105. static void Print(int value, bool error) { }
  106. static void Print(long long value, bool error) { }
  107. static void Print(unsigned value, bool error) { }
  108. static void Print(unsigned long long value, bool error) { }
  109. static void Print(float value, bool error) { }
  110. static void Print(bool value, bool error) { }
  111. static void Print(const Variant& value, bool error) { }
  112. static void PrintCallStack(bool error) { }
  113. static void LogWrite(const String& str, bool error, Log* ptr) { }
  114. static void LogTrace(const String& str, Log* ptr) { }
  115. static void LogDebug(const String& str, Log* ptr) { }
  116. static void LogInfo(const String& str, Log* ptr) { }
  117. static void LogWarning(const String& str, Log* ptr) { }
  118. static void LogError(const String& str, Log* ptr) { }
  119. #endif
  120. static void RegisterLog(asIScriptEngine* engine)
  121. {
  122. engine->RegisterGlobalProperty("const int LOG_TRACE", (void*)&LOG_TRACE);
  123. engine->RegisterGlobalProperty("const int LOG_DEBUG", (void*)&LOG_DEBUG);
  124. engine->RegisterGlobalProperty("const int LOG_INFO", (void*)&LOG_INFO);
  125. engine->RegisterGlobalProperty("const int LOG_WARNING", (void*)&LOG_WARNING);
  126. engine->RegisterGlobalProperty("const int LOG_ERROR", (void*)&LOG_ERROR);
  127. engine->RegisterGlobalProperty("const int LOG_NONE", (void*)&LOG_NONE);
  128. RegisterObject<Log>(engine, "Log");
  129. engine->RegisterObjectMethod("Log", "void Open(const String&in)", asMETHOD(Log, Open), asCALL_THISCALL);
  130. engine->RegisterObjectMethod("Log", "void Close()", asMETHOD(Log, Close), asCALL_THISCALL);
  131. engine->RegisterObjectMethod("Log", "void Write(const String&in, bool error = false)", asFUNCTION(LogWrite), asCALL_CDECL_OBJLAST);
  132. engine->RegisterObjectMethod("Log", "void Trace(const String&in)", asFUNCTION(LogTrace), asCALL_CDECL_OBJLAST);
  133. engine->RegisterObjectMethod("Log", "void Debug(const String&in)", asFUNCTION(LogDebug), asCALL_CDECL_OBJLAST);
  134. engine->RegisterObjectMethod("Log", "void Info(const String&in)", asFUNCTION(LogInfo), asCALL_CDECL_OBJLAST);
  135. engine->RegisterObjectMethod("Log", "void Warning(const String&in)", asFUNCTION(LogWarning), asCALL_CDECL_OBJLAST);
  136. engine->RegisterObjectMethod("Log", "void Error(const String&in)", asFUNCTION(LogError), asCALL_CDECL_OBJLAST);
  137. engine->RegisterObjectMethod("Log", "void set_level(int)", asMETHOD(Log, SetLevel), asCALL_THISCALL);
  138. engine->RegisterObjectMethod("Log", "int get_level() const", asMETHOD(Log, GetLevel), asCALL_THISCALL);
  139. engine->RegisterObjectMethod("Log", "void set_timeStamp(bool)", asMETHOD(Log, SetTimeStamp), asCALL_THISCALL);
  140. engine->RegisterObjectMethod("Log", "bool get_timeStamp() const", asMETHOD(Log, GetTimeStamp), asCALL_THISCALL);
  141. engine->RegisterObjectMethod("Log", "String get_lastMessage()", asMETHOD(Log, GetLastMessage), asCALL_THISCALL);
  142. engine->RegisterObjectMethod("Log", "void set_quiet(bool)", asMETHOD(Log, SetQuiet), asCALL_THISCALL);
  143. engine->RegisterObjectMethod("Log", "bool get_quiet() const", asMETHOD(Log, IsQuiet), asCALL_THISCALL);
  144. engine->RegisterGlobalFunction("Log@+ get_log()", asFUNCTION(GetLog), asCALL_CDECL);
  145. // Register also Print() functions for convenience
  146. engine->RegisterGlobalFunction("void Print(const String&in, bool error = false)", asFUNCTIONPR(Print, (const String&, bool), void), asCALL_CDECL);
  147. engine->RegisterGlobalFunction("void Print(int, bool error = false)", asFUNCTIONPR(Print, (int, bool), void), asCALL_CDECL);
  148. engine->RegisterGlobalFunction("void Print(int64, bool error = false)", asFUNCTIONPR(Print, (long long, bool), void), asCALL_CDECL);
  149. engine->RegisterGlobalFunction("void Print(uint, bool error = false)", asFUNCTIONPR(Print, (unsigned, bool), void), asCALL_CDECL);
  150. engine->RegisterGlobalFunction("void Print(uint64, bool error = false)", asFUNCTIONPR(Print, (unsigned long long, bool), void), asCALL_CDECL);
  151. engine->RegisterGlobalFunction("void Print(float, bool error = false)", asFUNCTIONPR(Print, (float, bool), void), asCALL_CDECL);
  152. engine->RegisterGlobalFunction("void Print(bool, bool error = false)", asFUNCTIONPR(Print, (bool, bool), void), asCALL_CDECL);
  153. engine->RegisterGlobalFunction("void Print(const Variant&in, bool error = false)", asFUNCTIONPR(Print, (const Variant&, bool), void), asCALL_CDECL);
  154. engine->RegisterGlobalFunction("void PrintCallStack(bool error = false)", asFUNCTION(PrintCallStack), asCALL_CDECL);
  155. }
  156. static File* ConstructFile()
  157. {
  158. return new File(GetScriptContext());
  159. }
  160. static File* ConstructFileAndOpen(const String& fileName, FileMode mode)
  161. {
  162. return new File(GetScriptContext(), fileName, mode);
  163. }
  164. static NamedPipe* ConstructNamedPipe()
  165. {
  166. return new NamedPipe(GetScriptContext());
  167. }
  168. static NamedPipe* ConstructNamedPipeAndOpen(const String& fileName, bool isServer)
  169. {
  170. return new NamedPipe(GetScriptContext(), fileName, isServer);
  171. }
  172. static void ConstructVectorBuffer(VectorBuffer* ptr)
  173. {
  174. new(ptr) VectorBuffer();
  175. }
  176. static void ConstructVectorBufferCopy(const VectorBuffer& buffer, VectorBuffer* ptr)
  177. {
  178. new(ptr) VectorBuffer(buffer);
  179. }
  180. static void ConstructVectorBufferFromStream(Deserializer* src, unsigned size, VectorBuffer* ptr)
  181. {
  182. if (src)
  183. new(ptr) VectorBuffer(*src, size);
  184. else
  185. new(ptr) VectorBuffer();
  186. }
  187. static void DestructVectorBuffer(VectorBuffer* ptr)
  188. {
  189. ptr->~VectorBuffer();
  190. }
  191. static void VectorBufferSetData(Deserializer* src, unsigned size, VectorBuffer* ptr)
  192. {
  193. if (src)
  194. ptr->SetData(*src, size);
  195. }
  196. static unsigned char* VectorBufferAt(unsigned index, VectorBuffer* ptr)
  197. {
  198. if (index >= ptr->GetSize())
  199. {
  200. asGetActiveContext()->SetException("Index out of bounds");
  201. return nullptr;
  202. }
  203. return ptr->GetModifiableData() + index;
  204. }
  205. static void ConstructVariantVectorBuffer(const VectorBuffer& value, Variant* ptr)
  206. {
  207. new(ptr) Variant(value);
  208. }
  209. static VectorBuffer VariantGetBuffer(Variant* ptr)
  210. {
  211. VectorBuffer ret(ptr->GetBuffer());
  212. return ret;
  213. }
  214. static FileSystem* GetFileSystem()
  215. {
  216. return GetScriptContext()->GetSubsystem<FileSystem>();
  217. }
  218. static CScriptArray* FileSystemScanDir(const String& pathName, const String& filter, unsigned flags, bool recursive, FileSystem* ptr)
  219. {
  220. Vector<String> result;
  221. ptr->ScanDir(result, pathName, filter, flags, recursive);
  222. return VectorToArray<String>(result, "Array<String>");
  223. }
  224. static int FileSystemSystemRun(const String& fileName, CScriptArray* srcArguments, FileSystem* ptr)
  225. {
  226. if (!srcArguments)
  227. return -1;
  228. unsigned numArguments = srcArguments->GetSize();
  229. Vector<String> destArguments(numArguments);
  230. for (unsigned i = 0; i < numArguments; ++i)
  231. destArguments[i] = *(static_cast<String*>(srcArguments->At(i)));
  232. return ptr->SystemRun(fileName, destArguments);
  233. }
  234. static unsigned FileSystemSystemRunAsync(const String& fileName, CScriptArray* srcArguments, FileSystem* ptr)
  235. {
  236. if (!srcArguments)
  237. return M_MAX_UNSIGNED;
  238. unsigned numArguments = srcArguments->GetSize();
  239. Vector<String> destArguments(numArguments);
  240. for (unsigned i = 0; i < numArguments; ++i)
  241. destArguments[i] = *(static_cast<String*>(srcArguments->At(i)));
  242. return ptr->SystemRunAsync(fileName, destArguments);
  243. }
  244. static void RegisterSerialization(asIScriptEngine* engine)
  245. {
  246. engine->RegisterEnum("FileMode");
  247. engine->RegisterEnumValue("FileMode", "FILE_READ", FILE_READ);
  248. engine->RegisterEnumValue("FileMode", "FILE_WRITE", FILE_WRITE);
  249. engine->RegisterEnumValue("FileMode", "FILE_READWRITE", FILE_READWRITE);
  250. engine->RegisterGlobalProperty("const uint SCAN_FILES", (void*)&SCAN_FILES);
  251. engine->RegisterGlobalProperty("const uint SCAN_DIRS", (void*)&SCAN_DIRS);
  252. engine->RegisterGlobalProperty("const uint SCAN_HIDDEN", (void*)&SCAN_HIDDEN);
  253. engine->RegisterObjectType("VectorBuffer", sizeof(VectorBuffer), asOBJ_VALUE | asOBJ_APP_CLASS_CDK);
  254. engine->RegisterObjectType("Serializer", 0, asOBJ_REF);
  255. engine->RegisterObjectBehaviour("Serializer", asBEHAVE_ADDREF, "void f()", asFUNCTION(FakeAddRef), asCALL_CDECL_OBJLAST);
  256. engine->RegisterObjectBehaviour("Serializer", asBEHAVE_RELEASE, "void f()", asFUNCTION(FakeReleaseRef), asCALL_CDECL_OBJLAST);
  257. RegisterSerializer<Serializer>(engine, "Serializer");
  258. engine->RegisterObjectType("Deserializer", 0, asOBJ_REF);
  259. engine->RegisterObjectBehaviour("Deserializer", asBEHAVE_ADDREF, "void f()", asFUNCTION(FakeAddRef), asCALL_CDECL_OBJLAST);
  260. engine->RegisterObjectBehaviour("Deserializer", asBEHAVE_RELEASE, "void f()", asFUNCTION(FakeReleaseRef), asCALL_CDECL_OBJLAST);
  261. RegisterDeserializer<Deserializer>(engine, "Deserializer");
  262. RegisterObject<File>(engine, "File");
  263. engine->RegisterObjectBehaviour("File", asBEHAVE_FACTORY, "File@+ f()", asFUNCTION(ConstructFile), asCALL_CDECL);
  264. engine->RegisterObjectBehaviour("File", asBEHAVE_FACTORY, "File@+ f(const String&in, FileMode mode = FILE_READ)", asFUNCTION(ConstructFileAndOpen), asCALL_CDECL);
  265. engine->RegisterObjectMethod("File", "bool Open(const String&in, FileMode mode = FILE_READ)", asMETHODPR(File, Open, (const String&, FileMode), bool), asCALL_THISCALL);
  266. engine->RegisterObjectMethod("File", "void Close()", asMETHOD(File, Close), asCALL_THISCALL);
  267. engine->RegisterObjectMethod("File", "FileMode get_mode() const", asMETHOD(File, GetMode), asCALL_THISCALL);
  268. engine->RegisterObjectMethod("File", "bool get_open()", asMETHOD(File, IsOpen), asCALL_THISCALL);
  269. engine->RegisterObjectMethod("File", "bool get_packaged()", asMETHOD(File, IsPackaged), asCALL_THISCALL);
  270. RegisterSerializer<File>(engine, "File");
  271. RegisterDeserializer<File>(engine, "File");
  272. RegisterObject<NamedPipe>(engine, "NamedPipe");
  273. engine->RegisterObjectBehaviour("NamedPipe", asBEHAVE_FACTORY, "NamedPipe@+ f()", asFUNCTION(ConstructNamedPipe), asCALL_CDECL);
  274. engine->RegisterObjectBehaviour("NamedPipe", asBEHAVE_FACTORY, "NamedPipe@+ f(const String&in, bool)", asFUNCTION(ConstructNamedPipeAndOpen), asCALL_CDECL);
  275. engine->RegisterObjectMethod("NamedPipe", "bool Open(const String&in, bool)", asMETHODPR(NamedPipe, Open, (const String&, bool), bool), asCALL_THISCALL);
  276. engine->RegisterObjectMethod("NamedPipe", "void Close()", asMETHOD(NamedPipe, Close), asCALL_THISCALL);
  277. engine->RegisterObjectMethod("NamedPipe", "bool get_server() const", asMETHOD(NamedPipe, IsServer), asCALL_THISCALL);
  278. engine->RegisterObjectMethod("NamedPipe", "bool get_open() const", asMETHOD(NamedPipe, IsOpen), asCALL_THISCALL);
  279. RegisterSerializer<NamedPipe>(engine, "NamedPipe");
  280. RegisterDeserializer<NamedPipe>(engine, "NamedPipe");
  281. engine->RegisterObjectBehaviour("VectorBuffer", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructVectorBuffer), asCALL_CDECL_OBJLAST);
  282. engine->RegisterObjectBehaviour("VectorBuffer", asBEHAVE_CONSTRUCT, "void f(const VectorBuffer&in)", asFUNCTION(ConstructVectorBufferCopy), asCALL_CDECL_OBJLAST);
  283. engine->RegisterObjectBehaviour("VectorBuffer", asBEHAVE_CONSTRUCT, "void f(Deserializer@+, uint)", asFUNCTION(ConstructVectorBufferFromStream), asCALL_CDECL_OBJLAST);
  284. engine->RegisterObjectBehaviour("VectorBuffer", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructVectorBuffer), asCALL_CDECL_OBJLAST);
  285. engine->RegisterObjectMethod("VectorBuffer", "VectorBuffer& opAssign(const VectorBuffer&in)", asMETHODPR(VectorBuffer, operator =, (const VectorBuffer&), VectorBuffer&), asCALL_THISCALL);
  286. engine->RegisterObjectMethod("VectorBuffer", "void SetData(Deserializer@+, uint)", asFUNCTION(VectorBufferSetData), asCALL_CDECL_OBJLAST);
  287. engine->RegisterObjectMethod("VectorBuffer", "void Clear()", asMETHOD(VectorBuffer, Clear), asCALL_THISCALL);
  288. engine->RegisterObjectMethod("VectorBuffer", "void Resize(uint)", asMETHOD(VectorBuffer, Resize), asCALL_THISCALL);
  289. engine->RegisterObjectMethod("VectorBuffer", "uint8 &opIndex(uint)", asFUNCTION(VectorBufferAt), asCALL_CDECL_OBJLAST);
  290. engine->RegisterObjectMethod("VectorBuffer", "const uint8 &opIndex(uint) const", asFUNCTION(VectorBufferAt), asCALL_CDECL_OBJLAST);
  291. RegisterSerializer<VectorBuffer>(engine, "VectorBuffer");
  292. RegisterDeserializer<VectorBuffer>(engine, "VectorBuffer");
  293. // Register VectorBuffer conversions to Variant
  294. engine->RegisterObjectBehaviour("Variant", asBEHAVE_CONSTRUCT, "void f(const VectorBuffer&in)", asFUNCTION(ConstructVariantVectorBuffer), asCALL_CDECL_OBJLAST);
  295. engine->RegisterObjectMethod("Variant", "Variant& opAssign(const VectorBuffer&in)", asMETHODPR(Variant, operator =, (const VectorBuffer&), Variant&), asCALL_THISCALL);
  296. engine->RegisterObjectMethod("Variant", "bool opEquals(const VectorBuffer&in) const", asMETHODPR(Variant, operator ==, (const VectorBuffer&) const, bool), asCALL_THISCALL);
  297. engine->RegisterObjectMethod("Variant", "VectorBuffer GetBuffer() const", asMETHOD(Variant, GetVectorBuffer), asCALL_THISCALL);
  298. // Register VectorBuffer compression functions
  299. engine->RegisterGlobalFunction("VectorBuffer CompressVectorBuffer(VectorBuffer&in)", asFUNCTION(CompressVectorBuffer), asCALL_CDECL);
  300. engine->RegisterGlobalFunction("VectorBuffer DecompressVectorBuffer(VectorBuffer&in)", asFUNCTION(DecompressVectorBuffer), asCALL_CDECL);
  301. }
  302. void RegisterFileSystem(asIScriptEngine* engine)
  303. {
  304. RegisterObject<FileSystem>(engine, "FileSystem");
  305. engine->RegisterObjectMethod("FileSystem", "bool FileExists(const String&in) const", asMETHOD(FileSystem, FileExists), asCALL_THISCALL);
  306. engine->RegisterObjectMethod("FileSystem", "bool DirExists(const String&in) const", asMETHOD(FileSystem, DirExists), asCALL_THISCALL);
  307. engine->RegisterObjectMethod("FileSystem", "bool SetLastModifiedTime(const String&in, uint)", asMETHOD(FileSystem, SetLastModifiedTime), asCALL_THISCALL);
  308. engine->RegisterObjectMethod("FileSystem", "uint GetLastModifiedTime(const String&in) const", asMETHOD(FileSystem, GetLastModifiedTime), asCALL_THISCALL);
  309. engine->RegisterObjectMethod("FileSystem", "Array<String>@ ScanDir(const String&in, const String&in, uint, bool) const", asFUNCTION(FileSystemScanDir), asCALL_CDECL_OBJLAST);
  310. engine->RegisterObjectMethod("FileSystem", "bool CreateDir(const String&in)", asMETHOD(FileSystem, CreateDir), asCALL_THISCALL);
  311. engine->RegisterObjectMethod("FileSystem", "int SystemCommand(const String&in, bool redirectStdOutToLog = false)", asMETHOD(FileSystem, SystemCommand), asCALL_THISCALL);
  312. engine->RegisterObjectMethod("FileSystem", "int SystemRun(const String&in, Array<String>@+)", asFUNCTION(FileSystemSystemRun), asCALL_CDECL_OBJLAST);
  313. engine->RegisterObjectMethod("FileSystem", "uint SystemCommandAsync(const String&in)", asMETHOD(FileSystem, SystemCommandAsync), asCALL_THISCALL);
  314. engine->RegisterObjectMethod("FileSystem", "uint SystemRunAsync(const String&in, Array<String>@+)", asFUNCTION(FileSystemSystemRunAsync), asCALL_CDECL_OBJLAST);
  315. engine->RegisterObjectMethod("FileSystem", "bool SystemOpen(const String&in, const String&in)", asMETHOD(FileSystem, SystemOpen), asCALL_THISCALL);
  316. engine->RegisterObjectMethod("FileSystem", "bool Copy(const String&in, const String&in)", asMETHOD(FileSystem, Copy), asCALL_THISCALL);
  317. engine->RegisterObjectMethod("FileSystem", "bool Rename(const String&in, const String&in)", asMETHOD(FileSystem, Rename), asCALL_THISCALL);
  318. engine->RegisterObjectMethod("FileSystem", "bool Delete(const String&in)", asMETHOD(FileSystem, Delete), asCALL_THISCALL);
  319. engine->RegisterObjectMethod("FileSystem", "String GetAppPreferencesDir(const String&in org, const String&in app) const", asMETHOD(FileSystem, GetAppPreferencesDir), asCALL_THISCALL);
  320. engine->RegisterObjectMethod("FileSystem", "String get_currentDir() const", asMETHOD(FileSystem, GetCurrentDir), asCALL_THISCALL);
  321. engine->RegisterObjectMethod("FileSystem", "void set_currentDir(const String&in)", asMETHOD(FileSystem, SetCurrentDir), asCALL_THISCALL);
  322. engine->RegisterObjectMethod("FileSystem", "void set_executeConsoleCommands(bool)", asMETHOD(FileSystem, SetExecuteConsoleCommands), asCALL_THISCALL);
  323. engine->RegisterObjectMethod("FileSystem", "bool get_executeConsoleCommands() const", asMETHOD(FileSystem, GetExecuteConsoleCommands), asCALL_THISCALL);
  324. engine->RegisterObjectMethod("FileSystem", "String get_programDir() const", asMETHOD(FileSystem, GetProgramDir), asCALL_THISCALL);
  325. engine->RegisterObjectMethod("FileSystem", "String get_userDocumentsDir() const", asMETHOD(FileSystem, GetUserDocumentsDir), asCALL_THISCALL);
  326. engine->RegisterObjectMethod("FileSystem", "String get_temporaryDir() const", asMETHOD(FileSystem, GetTemporaryDir), asCALL_THISCALL);
  327. engine->RegisterGlobalFunction("FileSystem@+ get_fileSystem()", asFUNCTION(GetFileSystem), asCALL_CDECL);
  328. engine->RegisterGlobalFunction("String GetPath(const String&in)", asFUNCTION(GetPath), asCALL_CDECL);
  329. engine->RegisterGlobalFunction("String GetFileName(const String&in)", asFUNCTION(GetFileName), asCALL_CDECL);
  330. engine->RegisterGlobalFunction("String GetExtension(const String&in, bool lowercaseExtension = true)", asFUNCTION(GetExtension), asCALL_CDECL);
  331. engine->RegisterGlobalFunction("String GetFileNameAndExtension(const String&in, bool lowercaseExtension = false)", asFUNCTION(GetFileNameAndExtension), asCALL_CDECL);
  332. engine->RegisterGlobalFunction("String ReplaceExtension(const String&in, const String&in)", asFUNCTION(ReplaceExtension), asCALL_CDECL);
  333. engine->RegisterGlobalFunction("String AddTrailingSlash(const String&in)", asFUNCTION(AddTrailingSlash), asCALL_CDECL);
  334. engine->RegisterGlobalFunction("String RemoveTrailingSlash(const String&in)", asFUNCTION(RemoveTrailingSlash), asCALL_CDECL);
  335. engine->RegisterGlobalFunction("String GetParentPath(const String&in)", asFUNCTION(GetParentPath), asCALL_CDECL);
  336. engine->RegisterGlobalFunction("String GetInternalPath(const String&in)", asFUNCTION(GetInternalPath), asCALL_CDECL);
  337. engine->RegisterGlobalFunction("bool IsAbsolutePath(const String&in)", asFUNCTION(IsAbsolutePath), asCALL_CDECL);
  338. }
  339. static PackageFile* ConstructPackageFile()
  340. {
  341. return new PackageFile(GetScriptContext());
  342. }
  343. static PackageFile* ConstructAndOpenPackageFile(const String& fileName, unsigned startOffset)
  344. {
  345. return new PackageFile(GetScriptContext(), fileName, startOffset);
  346. }
  347. static const CScriptArray* PackageFileGetEntryNames(PackageFile* packageFile)
  348. {
  349. return VectorToArray<String>(packageFile->GetEntryNames(), "Array<String>");
  350. }
  351. static void RegisterPackageFile(asIScriptEngine* engine)
  352. {
  353. RegisterObject<PackageFile>(engine, "PackageFile");
  354. engine->RegisterObjectBehaviour("PackageFile", asBEHAVE_FACTORY, "PackageFile@+ f()", asFUNCTION(ConstructPackageFile), asCALL_CDECL);
  355. engine->RegisterObjectBehaviour("PackageFile", asBEHAVE_FACTORY, "PackageFile@+ f(const String&in, uint startOffset = 0)", asFUNCTION(ConstructAndOpenPackageFile), asCALL_CDECL);
  356. engine->RegisterObjectMethod("PackageFile", "bool Open(const String&in, uint startOffset = 0) const", asMETHOD(PackageFile, Open), asCALL_THISCALL);
  357. engine->RegisterObjectMethod("PackageFile", "bool Exists(const String&in) const", asMETHOD(PackageFile, Exists), asCALL_THISCALL);
  358. engine->RegisterObjectMethod("PackageFile", "const String& get_name() const", asMETHOD(PackageFile, GetName), asCALL_THISCALL);
  359. engine->RegisterObjectMethod("PackageFile", "uint get_numFiles() const", asMETHOD(PackageFile, GetNumFiles), asCALL_THISCALL);
  360. engine->RegisterObjectMethod("PackageFile", "uint get_totalSize() const", asMETHOD(PackageFile, GetTotalSize), asCALL_THISCALL);
  361. engine->RegisterObjectMethod("PackageFile", "uint get_totalDataSize() const", asMETHOD(PackageFile, GetTotalDataSize), asCALL_THISCALL);
  362. engine->RegisterObjectMethod("PackageFile", "uint get_checksum() const", asMETHOD(PackageFile, GetChecksum), asCALL_THISCALL);
  363. engine->RegisterObjectMethod("PackageFile", "bool compressed() const", asMETHOD(PackageFile, IsCompressed), asCALL_THISCALL);
  364. engine->RegisterObjectMethod("PackageFile", "Array<String>@ GetEntryNames() const", asFUNCTION(PackageFileGetEntryNames), asCALL_CDECL_OBJLAST);
  365. }
  366. void RegisterIOAPI(asIScriptEngine* engine)
  367. {
  368. RegisterLog(engine);
  369. RegisterSerialization(engine);
  370. RegisterFileSystem(engine);
  371. RegisterPackageFile(engine);
  372. }
  373. }