Resource.hx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is 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
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe;
  23. import php.*;
  24. import haxe.io.Bytes;
  25. import haxe.crypto.Base64;
  26. @:coreApi
  27. class Resource {
  28. static function cleanName(name:String):String {
  29. return ~/[\\\/:?"*<>|]/.replace(name, '_');
  30. }
  31. static function getDir():String {
  32. var pathToRoot = '/../..';
  33. #if php_prefix
  34. pathToRoot += '/..';
  35. for (i in 0...Global.substr_count(Boot.getPrefix(), '\\')) {
  36. pathToRoot += '/..';
  37. }
  38. #end
  39. return Global.dirname(Const.__FILE__) + pathToRoot + "/res";
  40. }
  41. @:access(haxe.io.Path.escape)
  42. static function getPath(name:String):String {
  43. return getDir() + '/' + haxe.io.Path.escape(name);
  44. }
  45. @:access(haxe.io.Path.unescape)
  46. public static function listNames():Array<String> {
  47. var a = sys.FileSystem.readDirectory(getDir());
  48. if (a[0] == '.')
  49. a.shift();
  50. if (a[0] == '..')
  51. a.shift();
  52. return a.map(function(s) return haxe.io.Path.unescape(s));
  53. }
  54. public static function getString(name:String):String {
  55. var path = getPath(name);
  56. return if (!sys.FileSystem.exists(path))
  57. null;
  58. else
  59. sys.io.File.getContent(path);
  60. }
  61. public static function getBytes(name:String):haxe.io.Bytes {
  62. var path = getPath(name);
  63. return if (!sys.FileSystem.exists(path))
  64. null;
  65. else
  66. sys.io.File.getBytes(path);
  67. }
  68. }