Accessing StreamingAssets resources from C++ DLL in Unity on Android - c#

I have developed a C# application in Unity for Android. Additionally, I built a custom external C++ DLL which is properly loaded and accessible from the C# code. I have also added some .xml files into the StreamingAssets folder which works as expected as I can see those files loaded into the .apk.
Currently my problem is that I'm not able to access such xml files from my C++ library. On one hand I'm not sure how to do it correctly, on the other I tried multiple unsuccessful approaches including trying to understand in which cwd was my DLL loaded and then navigate to the files I needed (which didn't work as I constantly receive an access denied error).
EDIT:
Your replies actually put me on the right track and I solved the issue as follow:
During the OnStart() phase of my application, I use the UnityWebRequest class to extract from the JAR file the required files and then I save them in the Application.persistentDataPath
After that, I pass to my DLL the new path to each file which allows me to access them without any issue.

On Android the StreamingAssets are in a jar. That may well be the reason why you can't access them, but as you can see in the manual you can use the WWW class to retrieve files there.
On Android, the files are contained within a compressed .jar file
(which is essentially the same format as standard zip-compressed
files). This means that if you do not use Unity’s WWW class to
retrieve the file, you need to use additional software to see inside
the .jar archive and obtain the file.
(https://docs.unity3d.com/Manual/StreamingAssets.html)

On Android, the files are contained within a compressed .jar file
(which is essentially the same format as standard zip-compressed
files). This means that if you do not use Unity’s WWW class to
retrieve the file, you need to use additional software to see inside
the .jar archive and obtain the file.
A JAR is a package file format typically used to aggregate many Java class files and associated metadata and resources into one file for distribution. JAR files are archive files that include a Java-specific manifest file. They are built on the ZIP format and typically have a .jar file extension.
This means that you can work with jar files the same way as you do with zip files.
There is a fork for minizip with examples on how to use it.

Related

Reading files in xamarin forms as a src folder

I'm trying to read a file.txt from a src folder inside my project. But when I write File.ReadAllText("PATH") I don't know the path for the file. The file is also inside a views folder.
string text = File.ReadAllText();
This is my project folder:
I'm trying to read from popuppage.xaml to src/.txt file
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. Previously, this meant that reading and writing files was most easily performed using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
You can check this document to learn more about File Handing in Xamarin Forms https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows

How to merge a file to a precompiled exe application

I have designed and created two seperate windows application ( say winapp1 and winapp2). Winapp1 creates a file(say file.txt) in the disk containing some text. And I need to merge this file.txt with precompiled version of winapp2 ie winapp2.exe so that the winapp2 can display it.
Winapp1 creates a file with different contents each execution time. And for each file there will be a copy of winapp2 that needs to be merged.
Is there a way to this? If there is, help me.
And my English is not so good. Thanks for reading this.
The easiest solution, (assuming that releasing the source code for winapp2 to the system that runs winapp1 doesn't present problems) is to have the entire source for the winapp2 project accessible to winapp1. The source should include a blank copy of the file as an embedded resource. Winapp1 can then modify the source file and use MSBuild to build a fresh copy of Winapp2.
To modify the text file, you should be able to just use the normal file manipulation .NET methods and to call MSBuild, you may be able to do it via a .NET class, but you can certainly just call the msbuild executable from the relevant .NET framework folder in %windir%\Microsoft.NET\Framework\ and pass in the full path of the project file.

Use external existing file as resource file in .NET

I'm writing some integration tests for some SQL scripts that live in a folder separate from the project. Since the setup of the machine I'm writing on the tests on differs from the machine they will be run on, I would like to include them as resource files rather than hard coding any paths. However the default behavior for adding an existing file as a resource file simply copies the file, which is not what I want in case any of the SQL scripts get updated.
Does anyone know the best way to get the resource file to actually reference the SQL scripts that are in a folder separate from the project, or to somehow copy them into the assembly at compile time so I don't have to load them via absolute/relative paths?
Edit: For clarity, I'm trying to get the resource file to act as a symlink to the original file at compile time. Adding an existing file to a resource in file in Visual Studio simply makes a copy of the file, which means any changes to the original are not propagated to the resource file.
After asking on IRC, someone guided me to what I was looking for. You are able to add an existing file to a project as a link (there is an arrow on the Add box when in the file dialog), and set it's Build Action property to Embedded Resource
https://support.microsoft.com/en-us/kb/306234
Daniel posted a link on how to actually read an embedded resource:
How to read embedded resource text file

Add Text file to assets folder after export apk file with C#

Just assume some persons's information exist in Asp.net developed application.
and also another application have been written to display the data of mentioned persons for android(by Eclipse).
How could make change in "APK" file after adding data for new Person by Asp to get the data of mentioned person from website.
How mentioned person ID could be placed as text file in assets folder while APK SIGN dont interfere it.
I want have personal apk for each person and personal apk created(or modified) by Asp website(This APK contain a text file with MyPersonId and it added to ASSETS folder with C#).
I was trying to do the same, and i'm almost there.
Basically i used MIT AppInventor to create an APK for Android. On it, i created a TXT file in the assets folder. The MIT AppInventor compiled the code to create the APK file for distribution.
I than placed the APK in a folder, owned by an ASP.net page. The goal would be to open the APK file (as a Zip, currently using the ZipSharpLib), locate the "info.txt" on the assets folder, and replace it for another.
My problem so far is on the "re-pack" ou "re-compress" the copy of the file; android is not recognizing it as a valid APK.
So, up to the "decompress" the APK, run every entry, locating the one that is the asset to replace i'm good (just use ZipSharpLib, classes ZipFile and ZipEntry - they show you how on their site). But on the "compress" a new changed file, i'm failing.
To do this kind of things you would need help of a decompilation and recompilation tool such as ApkTool. You can't just zip back the files and hope android recognize zip format as an apk format immediately. There's a signing process included to make it a "real apk" again

Embed exe file in a Class library

Is it possible to embed an exe file in a class library (dll file)?
I want to use this exe without the necessity of copying it manually to my workstation. In other words, if I want to use my C# class library in another app, I won't need to copy these exe files in a folder and pass the path of this folder in my app.
If there is a way how to do this, it would be great.
sorry but what I mean is just, I made a library for screenshots using Selenium Webdriver and when creating a new webdriver object, I need to pass the exe file of the webdriver.
what I need the most is that I don't want to copy the exe file if I will use this library to another workstation for example, I want everything to be packaged as one file
Thank you
Well, technically you can embed a binary file as a resource within a DLL (by adding the file as a binary resource through the project properties), but you'll still need to save the file to disk in order to execute it (which is assume what you're trying to do) and will possibly have security issues unless your application is fully trusted.
If the binary file is a resource you can extract the bytes from the static Properties class:
byte[] exe = Properties.Resources.MyExe;
and save it to disk like any other byte array.
If you own the code for the EXE then it would be a lot better for you to turn your EXE code into a library. Then you'll be able to refer to that library from anywhere and call any public functionnality it has. It's a far, far, FAR better approach.
Should you still need to run that code as a standalone process, nothing prevents you from making a new EXE front that will refer to that same library.
Now if you do not have the code, then depending on your deployment strategy you may prefer creating a reusable deployment component / module that can be attached to other application setups.

Categories

Resources