ios: implement TextAsset for iOS
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 15 Dec 2017 20:47:12 +0000 (15:47 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Wed, 17 Jan 2018 20:30:11 +0000 (15:30 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/gamnit/gamnit_ios.nit
lib/ios/assets.nit [new file with mode: 0644]

index 1a678f1..4ee0a98 100644 (file)
@@ -18,6 +18,8 @@ module gamnit_ios
 import ios
 import gamnit
 
+import ios::assets
+
 redef class App
        redef fun did_finish_launching_with_options
        do
diff --git a/lib/ios/assets.nit b/lib/ios/assets.nit
new file mode 100644 (file)
index 0000000..1c73603
--- /dev/null
@@ -0,0 +1,51 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Implementation of `app::assets`
+module assets
+
+import cocoa
+import app::assets
+
+redef class TextAsset
+       redef fun load
+       do
+               # Find file
+               var ns_path = ("assets"/path).to_nsstring
+               var path_in_bundle = asset_path(ns_path)
+               if path_in_bundle.address_is_null then
+                       self.error = new Error("TextAsset at '{path}' not found")
+                       self.to_s = ""
+                       return ""
+               end
+
+               # Load content
+               var text = asset_content(path_in_bundle)
+               if text.address_is_null then
+                       self.error = new Error("Failed to read content of TextAsset at '{path}'")
+                       self.to_s = ""
+                       return ""
+               end
+
+               return text.to_s
+       end
+end
+
+private fun asset_path(path: NSString): NSString in "ObjC" `{
+       return [[NSBundle mainBundle] pathForResource:path ofType:nil];
+`}
+
+private fun asset_content(path: NSString): NSString in "ObjC" `{
+       return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
+`}