Merge: Gamnit on iOS
[nit.git] / lib / ios / assets.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Implementation of `app::assets`
16 module assets
17
18 import cocoa
19 import app::assets
20
21 redef class TextAsset
22 redef fun load
23 do
24 # Find file
25 var ns_path = ("assets"/path).to_nsstring
26 var path_in_bundle = asset_path(ns_path)
27 if path_in_bundle.address_is_null then
28 self.error = new Error("TextAsset at '{path}' not found")
29 self.to_s = ""
30 return ""
31 end
32
33 # Load content
34 var text = asset_content(path_in_bundle)
35 if text.address_is_null then
36 self.error = new Error("Failed to read content of TextAsset at '{path}'")
37 self.to_s = ""
38 return ""
39 end
40
41 return text.to_s
42 end
43 end
44
45 private fun asset_path(path: NSString): NSString in "ObjC" `{
46 return [[NSBundle mainBundle] pathForResource:path ofType:nil];
47 `}
48
49 private fun asset_url(path: NSString): NSObject in "ObjC" `{
50 return [[NSBundle mainBundle] URLForResource:path withExtension:nil];
51 `}
52
53 private fun asset_content(path: NSString): NSString in "ObjC" `{
54 return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
55 `}