libGDX FileHandle file not found on ios - ios

I have bin folder in my android assets folder in which I store json file for my default values for my game.
I read this file using
FileHandle gameDataFile = Gdx.files.local("assets/bin/levels_data");
and this work perfect on android phones. Today I try to export my game for ios and I get this error:
File not found: assets/bin/levels_data (Local)
I tried to remove assets from path but I get same error again (File not found: bin/levels_data (Local))
Can some one tell me how to solve this, how to read from iso, file that is located in assets folder?
I want to mentioned that asset loader loads all other filed(images, textures, sounds...) that are located in assets folder on ios . Problem is when I try to read file with Gdx.files.local but only on ios device ( simulator for now)
Thanks

To access internal package files (like assets on Android), you have to use internal (not local) file handle. Try this:
FileHandle gameDataFile = Gdx.files.internal("bin/levels_data");

Related

native script: WebView Interface plugin unable to find local resource

I am using webViewInterface plugin in nativescript 8 with webpack v5 I am trying to launch local downloaded file ex. video, audio, HTML files
I am able to download those files but while running that file with the help of webview I am getting
file not found
chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property '_onNativeEvent' of undefined", source: chrome-error://chromewebdata/
In NativeScript, by default, the tilde (~) marks the app root folder (note the App and not the Project!). So if my file is located in <project-name>/app/index.html then I need to set a path like this ~/index.html.
this was working fine with natviescript v6 but after upgrading to nativescript v8 it broke.
can anyone please help???
The app folder is read-only on iOS (at least, on real devices, not necessarily on emulators). You'll need to download the files to either the temp or documents folder and then access from there.
As a reference, see my own struggle with this issue.
OK i have what seems to be the same issue. In my case when i migrated from nativescript 7 to 8 the webpack.config.ts was cleaned up.
In there was something useful that copied all my webview files.
I added some code to my webpack.config.js and now it seems to work
"src" is the folder containing my webview folder.
const webpack = require("#nativescript/webpack");
module.exports = (env) => {
webpack.init(env);
webpack.Utils.addCopyRule({
from: 'webviews/**',
context : 'src'
})
return webpack.resolveConfig();
};
The doc I used.
NS 8 : https://docs.nativescript.org/webpack.html#adding-a-copy-rule
Previous plugin used : https://webpack.js.org/plugins/copy-webpack-plugin/

How do I put content into the iOS Documents folder at compile time, using Xamarin?

I have a data file that I need to include with my app when I distribute it. When loading any files in the app, I prefix the file name with:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
This works great for anything I create within the app (and for reading back), like files I download in response to a user action. But I can't for the life of me figure out how to place files there when I build my app in Visual Studio.
I've tried making a "Documents" subdirectory in the special "Resources" folder, but that didn't work (I tried setting the "Build Action" to both BundleResource and Content). When I look at the folder for my app (from using the simulator) I can see that in the "Documents" folder there's all the files I downloaded, but I can't find my data file that I'm trying to bundle ahead of time. I even searched my entire hard drive on the Mac and still couldn't find said data file.
The data file isn't an image, if it matters. Just raw binary data. How do I set it up so that this file goes into the proper documents directory at compile time, so that I can read it using the SpecialFolder.MyDocuments prefix? Thanks.
You can't. You can include files in your app bundle, and then at startup copy them from the bundle into a user folder. But this won't happen automatically.

How to load json file from arbitrary folder inside project?

How to load json file from arbitrary folder inside project ?
I have data folder and inside file user_data.json, but system.DocumentsDirectory points to another ( I have copied from net, I am pretty new to lua and corona)
function custom_load( strFilename )
local path = system.pathForFile( strFilename, system.DocumentsDirectory )
local file = io.open( path, "r" )
if file then
local content = file:read( "*a" )
io.close( file )
return contents
else
return ''
end
end
Your file has to exist before you can read it. Perhaps a brief description of the Corona SDK folder structure will help you understand what's going on.
All apps, regardless of iOS, Android or Corona while running in the simulator are made of up an "Application Bundle". In iOS terms, this is the .app file that Corona SDK produces. In Android, it's the .apk file. The files in the folder with your main.lua and any folders you create there are part of this app bundle. For security purposes, this folder is "Read Only" to your app. Corona SDK allows you to reference files in this folder as system.ResourceDirectory.
Once the app is created and running on the device, Three other folders are created in your App's Sandbox. This is an area that only your app can access. These are:
system.DocumentsDirectory, a readable and writable folder to store things you want to keep around with your app. This is where you would save settings files, files that your users create with your app and so on.
system.TemporaryDirectory, a readable and writeable folder to store things you don't expect to be there. It's where you can download files to that you intend to throw away.
Apple's iOS has a 3rd folder that Corona calls system.CachesDirectory which like system.TemporaryDirectory has not guarantees on now long files that are stored there will last, but the intent from Apple is if you can download it from the net and it gets deleted, you can always download it again. On Android, system.CachesDirectory and system.TemporaryDirectory are the same folders.
Since the assumption is the app is the only thing that can write to it's sandbox, files in system.DocumentsDirectory have to be created by the app. You can't just put a file there (okay there are ways in particular on the simulator, it's just a folder on your Mac or PC if you know where to look, but that's not realistic of a user loading your app) so your app has to create the file in system.DocumentsDirectory.
If the file has not been created, then trying to use io.open() in "read" mode ( the "r" ) to open a file will return nil because the file does not exist.
Dont worry its very simply , just refer this code you will automatically code after reading this.
https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK/blob/master/main.lua
In pathForFile use
local path = system.pathForFile( "data/user_data.json", system.ResourceDirectory)

NULL when using fopen with xcode

I am trying to initialize a multidimensional array from a file using C for a iPhone 4inch app but I can't open up the file using fopen.
Whenever I try this I get a NULL:
FILE *f;
f=fopen("/level1.rez", "r");
if (f == NULL)
{
printf("Error Reading File\n");
//exit (0);
}
I am not sure how to open files using C.
I tried this already:
I printed out the current working directory using getcwd but all I got was "/" and when I attached that to the file name I still got NULL.
I read that if you go to product > scheme > edit scheme then options you can change the current working directory but I don't see that option.
Also I read that you can use absolute paths like: /users/name/desktop/program
but I am new to iOS development so I don't know if that is a good idea.
So how do I get fopen to work?
You CAN specify absolute paths in iOS, but the path in your example is probably used in Mac OS, which is laid out a little differently. You can specify paths to fopen() as you say, but there is more work to finding out what the first part of that path really is.
The iOS puts all AppStore apps into folders with randomly generated sandbox directory names. It is basically the the hexadecimal string of a GUID. So you need to use methods from iOS frameworks to get the first part of the path (or URL) to your file.
If the file is part of the app bundle so it can ship with the app, then you will need to use NSBundle methods to find the path to the file.
If the file is generated or downloaded after the app starts up on the device, then you need to use NSFileManager methods to determine the path to the directory of the file. (Typically the Documents directory. You can build a directory structure of your choice within the sandbox.)

access assets from monodroid class library

Is it possible to access files (with build action "AndroidAsset") from a monodroid class library in a monodroid application that references the class library ?
I have created an "Assets" folder in the class lib and added a text file with build action "AndroidAsset", but from the app I could not access it via Assets.Open("file.txt");
I was hoping that the Assets from the class lib and the main application could somehow be "merged"...
No - you a can't access the assets from a class lib - only from the main exe project.
This is similar to the situation with Java Android projects.
One route around this might be to use embedded resources instead - see the answer to this question I asked on xamarin's forums - http://forums.xamarin.com/discussion/186/is-there-a-cross-platform-way-to-include-string-resources-from-class-library-projects - note that I haven't tried this yet
Is it possible to access files (with build action "AndroidAsset") from a monodroid class library in a monodroid application that references the class library ?
Yes. Just access them "as normal" via Context.Assets.Open. There is only one source of assets in the application, so any component can access any and all assets, if they have access to an AssetManager instance.
However, that just takes what you're asking at face-value. Can a Library project use the Context.Assets property? Certainly, if you provide it a Context instance.
But can a Library project provide it's own assets for inclusion into the application? No.
I have created an "Assets" folder in the class lib and added a text file with build action "AndroidAsset", but from the app I could not access it via Assets.Open("file.txt");
Library projects cannot provide assets that will be included in the larger application. Java doesn't support this either, afaik.
Things to remember
1 Build Action is AndroidAsset (Properties)
2 Copy to output directory Do not copy (Properties)
3 Add the file to the Assets folder
var destinationPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "yourfilename.txt");
using (Stream source = Assets.Open("yourfilename.txt"))
using (var dest = System.IO.File.Create (destinationPath)) {
source.CopyTo (dest);
}
Sometimes this will still fail, monodroid bug. The best thing to do is check to see if the files were included correctly.
Load up Eclipse, in the device browser, you should see your simulator.
Go to the directory data/app/ download the file yourappname.apk
This is just a zip file, so change the extension to .zip and open the
zip archive. And goto the folder /assets and see if the file you are trying
to load is in there.
Also if you try to grab a file off a physical device your testing on, some allow it and other don't. This is easiest to test on the simulator.
It is possible to access the Assets files from a library:
var sourcePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
//var files = Directory.GetFiles(sourcePath);
var filePath = Path.Combine(sourcePath, "MyAssetFile.bin");
var content = File.ReadAllBytes(filePath);

Resources