Resource.hx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C)2005-2012 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. #if as3
  24. @:coreApi
  25. class Resource {
  26. public static function listNames() : Array<String> untyped {
  27. return __keys__(__resources__.list);
  28. }
  29. public static function getString( name : String ) : String {
  30. var b = resolve(name);
  31. return b == null ? null : b.readUTFBytes(b.length);
  32. }
  33. public static function getBytes( name : String ) : haxe.io.Bytes {
  34. var b = resolve(name);
  35. return b == null ? null : haxe.io.Bytes.ofData(b);
  36. }
  37. static function resolve( name : String) :flash.utils.ByteArray untyped {
  38. var n = __resources__.list[name];
  39. if (n == null) return null;
  40. return untyped __new__(n);
  41. }
  42. static function __init__() : Void {
  43. untyped __resources__.__init__();
  44. }
  45. }
  46. #else
  47. @:coreApi
  48. class Resource {
  49. static var content : Array<{ name : String }>;
  50. public static function listNames() : Array<String> {
  51. var names = new Array();
  52. for( x in content )
  53. names.push(x.name);
  54. return names;
  55. }
  56. public static function getString( name : String ) : String {
  57. var b = resolve(name);
  58. return b == null ? null : b.readUTFBytes(b.length);
  59. }
  60. public static function getBytes( name : String ) : haxe.io.Bytes {
  61. var b = resolve(name);
  62. return b == null ? null : haxe.io.Bytes.ofData(b);
  63. }
  64. static function resolve( name : String ) : flash.utils.ByteArray {
  65. try untyped {
  66. var c = __as__(__global__["flash.utils.getDefinitionByName"]("_res._"+name.split(".").join("_")),Class);
  67. return __new__(c);
  68. } catch( e : Dynamic ) {
  69. return null;
  70. }
  71. }
  72. static function __init__() : Void {
  73. content = untyped __resources__();
  74. }
  75. }
  76. #end