Resource.hx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C)2005-2013 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. /**
  24. Resource can be used to access resources that were added through the
  25. -resource file@name command line parameter.
  26. Depending on their type they can be obtained as String through
  27. getString(name), or as binary data through getBytes(name).
  28. A list of all available resource names can be obtained from listNames().
  29. **/
  30. class Resource {
  31. #if (java || cs)
  32. @:keep static var content : Array<String>;
  33. #elseif python
  34. static var content : python.lib.Types.Dict<String, String>;
  35. #else
  36. static var content : Array<{ name : String, data : String, str : String }>;
  37. #end
  38. #if cs
  39. static var paths : haxe.ds.StringMap<String>;
  40. #if cs @:keep #end private static function getPaths():haxe.ds.StringMap<String>
  41. {
  42. if (paths != null)
  43. return paths;
  44. var p = new haxe.ds.StringMap();
  45. var all:cs.NativeArray<String> = untyped __cs__("typeof(haxe.Resource).Assembly.GetManifestResourceNames()");
  46. for (i in 0...all.Length)
  47. {
  48. var path = all[i];
  49. var name = path.substr(path.indexOf("Resources.") + 10);
  50. p.set(name, path);
  51. }
  52. return paths = p;
  53. }
  54. #end
  55. /**
  56. Lists all available resource names. The resource name is the name part
  57. of the -resource file@name command line parameter.
  58. **/
  59. public static function listNames() : Array<String> {
  60. var names = new Array();
  61. #if (java || cs)
  62. for ( x in content )
  63. names.push(x);
  64. #elseif python
  65. for ( k in content.keys().iter())
  66. names.push(k);
  67. #else
  68. for ( x in content )
  69. names.push(x.name);
  70. #end
  71. return names;
  72. }
  73. /**
  74. Retrieves the resource identified by `name` as a String.
  75. If `name` does not match any resource name, null is returned.
  76. **/
  77. public static function getString( name : String ) : String {
  78. #if java
  79. var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
  80. if (stream == null)
  81. return null;
  82. var stream = new java.io.NativeInput(stream);
  83. return stream.readAll().toString();
  84. #elseif cs
  85. var path = getPaths().get(name);
  86. var str:cs.system.io.Stream = untyped __cs__("typeof(haxe.Resource).Assembly.GetManifestResourceStream(path)");
  87. if (str != null)
  88. return new cs.io.NativeInput(str).readAll().toString();
  89. return null;
  90. #elseif python
  91. for( k in content.keys().iter() )
  92. if( k == name ) {
  93. var b : haxe.io.Bytes = haxe.crypto.Base64.decode(content.get(k, null));
  94. return b.toString();
  95. }
  96. return null;
  97. #else
  98. for( x in content )
  99. if( x.name == name ) {
  100. #if neko
  101. return new String(x.data);
  102. #else
  103. if( x.str != null ) return x.str;
  104. var b : haxe.io.Bytes = haxe.crypto.Base64.decode(x.data);
  105. return b.toString();
  106. #end
  107. }
  108. return null;
  109. #end
  110. }
  111. /**
  112. Retrieves the resource identified by `name` as an instance of
  113. haxe.io.Bytes.
  114. If `name` does not match any resource name, null is returned.
  115. **/
  116. public static function getBytes( name : String ) : haxe.io.Bytes {
  117. #if java
  118. var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
  119. if (stream == null)
  120. return null;
  121. var stream = new java.io.NativeInput(stream);
  122. return stream.readAll();
  123. #elseif cs
  124. var path = getPaths().get(name);
  125. var str:cs.system.io.Stream = untyped __cs__("typeof(haxe.Resource).Assembly.GetManifestResourceStream(path)");
  126. if (str != null)
  127. return new cs.io.NativeInput(str).readAll();
  128. return null;
  129. #elseif python
  130. for( k in content.keys().iter() )
  131. if( k == name ) {
  132. var b : haxe.io.Bytes = haxe.crypto.Base64.decode(content.get(k, null));
  133. return b;
  134. }
  135. return null;
  136. #else
  137. for( x in content )
  138. if( x.name == name ) {
  139. #if neko
  140. return haxe.io.Bytes.ofData(cast x.data);
  141. #else
  142. if( x.str != null ) return haxe.io.Bytes.ofString(x.str);
  143. return haxe.crypto.Base64.decode(x.data);
  144. #end
  145. }
  146. return null;
  147. #end
  148. }
  149. static function __init__() {
  150. #if neko
  151. var tmp = untyped __resources__();
  152. content = untyped Array.new1(tmp,__dollar__asize(tmp));
  153. #elseif php
  154. content = null;
  155. #elseif as3
  156. null;
  157. #elseif (java || cs)
  158. //do nothing
  159. #else
  160. content = untyped _hx_resources__();
  161. #end
  162. }
  163. }