Setup Database (SQLite) for Unity - c#

I have looked at too many tutorials to list and they all recommend the same thing. However, they have not helped to solve my problem.
I am trying to include in my project an SQLite DB, and when building for PC, MAC & Linux Standalone (testing on a Windows machine), the database works as expected. When testing on an Android device, I get the following errors.
E/Unity: ArgumentException: Invalid ConnectionString format for parameter "/storage/emulated/0/Android/data/com.tbltools.tbl_project/files/TBLDatabase.db"
at Mono.Data.Sqlite.SqliteConnection.ParseConnectionString (System.String connectionString) [0x00000] in <filename unknown>:0
at Mono.Data.Sqlite.SqliteConnection.Open () [0x00000] in <filename unknown>:0
at UIHandler+<RequestAllStudentNames>c__Iterator2.MoveNext () [0x00000] in <filename unknown>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00000] in <filename unknown>:0
I thought that making an amendment to the connectionString should be simple enough, but that hasn't solved my problem. This is what I have so far:
if (Application.platform != RuntimePlatform.Android)
{
// The name of the db.
tblDatabase = "URI=file:" + Application.dataPath + "/TBLDatabase.db"; //returns the complete path to database file exist.
}
else
{
tblDatabase = Application.persistentDataPath + "/TBLDatabase.db";
if (!File.Exists(tblDatabase))
{
// if it doesn't ->
Debug.LogWarning("File \"" + tblDatabase + "\" does not exist. Attempting to create from \"" + Application.dataPath + "!/assets/" + "TBLDatabase.db");
// open StreamingAssets directory and load the db ->
// #if UNITY_ANDROID
var loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "TBLDatabase.db"); // this is the path to your StreamingAssets in android
while (!loadDb.isDone) { } // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
// then save to Application.persistentDataPath
File.WriteAllBytes(tblDatabase, loadDb.bytes);
}
}
//open db connection
var connection = new SqliteConnection(tblDatabase);
connection.Open();
var command = connection.CreateCommand();
I have used adb shell and pulled the DB from my Android device and everything is as expected (the DB does exist and it isn't empty).
I believe I have all the relevant dll files, but if anyone could give me some guidance I would appreciate it.
***************************************************EDIT**********************************************
I have since made the following alterations based on advice given.
I am now calling the following method to start my connection and handle DB requestsStartCoroutine(RunDbCode(dbFileName, jsonStudentID, jsonIndiNames, jsonIndiStudentNumbers));
Then I have the following method:
IEnumerator RunDbCode(string fileName, List jsonStudentID, List jsonIndiNames, List jsonIndiStudentNumbers)
{
//Where to copy the db to
string dbDestination = Path.Combine(Application.persistentDataPath, "data");
dbDestination = Path.Combine(dbDestination, fileName);
//Check if the File do not exist then copy it
if (!File.Exists(dbDestination))
{
//Where the db file is at
string dbStreamingAsset = Path.Combine(Application.streamingAssetsPath, fileName);
byte[] result;
//Read the File from streamingAssets. Use WWW for Android
if (dbStreamingAsset.Contains("://") || dbStreamingAsset.Contains(":///"))
{
WWW www = new WWW(dbStreamingAsset);
yield return www;
result = www.bytes;
}
else
{
result = File.ReadAllBytes(dbStreamingAsset);
}
Debug.Log("Loaded db file");
//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(dbDestination)))
{
Directory.CreateDirectory(Path.GetDirectoryName(dbDestination));
}
//Copy the data to the persistentDataPath where the database API can freely access the file
File.WriteAllBytes(dbDestination, result);
Debug.Log("Copied db file");
}
//Now you can do the database operation
//open db connection
var connection = new SqliteConnection(dbDestination);
connection.Open();
var command = connection.CreateCommand();
// Drop the table if it already exists.
command.CommandText = "DROP TABLE IF EXISTS existing_individual;";
command.ExecuteNonQuery();
var sql = "CREATE TABLE existing_individual (studentID VARCHAR(23), fullName VARCHAR(50), studentNumber VARCHAR(20))";
command.CommandText = sql;
command.ExecuteNonQuery();
//Inserting the exisiting student names returned, into the SQLite DB
int count = 0;
foreach (var individuals in jsonStudentID)
{
//looping through the existing students registered for the individual quiz - below has been written to avoid SQL injection
sql = "INSERT INTO existing_individual (studentID, fullName, studentNumber) VALUES (#jsonStudentID, #jsonIndiNames, #jsonIndiStudentNumbers)";
command.Parameters.AddWithValue("#jsonStudentID", jsonStudentID[count]);
command.Parameters.AddWithValue("#jsonIndiNames", jsonIndiNames[count]);
command.Parameters.AddWithValue("#jsonIndiStudentNumbers", jsonIndiStudentNumbers[count]);
command.CommandText = sql;
command.ExecuteNonQuery();
count++;
}
//close the connection
command.Dispose();
command = null;
connection.Close();
connection = null;
}
However, I am still getting the following errors:
06-08 15:26:56.498 16300-16315/? E/Unity: ArgumentException: Invalid ConnectionString format for parameter "/storage/emulated/0/Android/data/com.tbltools.tbl_project/files/data/TBLDatabase.db"
at Mono.Data.Sqlite.SqliteConnection.ParseConnectionString (System.String connectionString) [0x00000] in <filename unknown>:0
at Mono.Data.Sqlite.SqliteConnection.Open () [0x00000] in <filename unknown>:0
at UIHandler+<RunDbCode>c__Iterator3.MoveNext () [0x00000] in <filename unknown>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00000] in <filename unknown>:0
UnityEngine.MonoBehaviour:StartCoroutineManaged2(IEnumerator)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
<RequestAllStudentNames>c__Iterator2:MoveNext()
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
(Filename: Line: -1)
06-08 15:26:56.502 16300-16315/? E/Unity: ArgumentException: Invalid ConnectionString format for parameter "URI"
at Mono.Data.Sqlite.SqliteConnection.ParseConnectionString (System.String connectionString) [0x00000] in <filename unknown>:0
at Mono.Data.Sqlite.SqliteConnection.Open () [0x00000] in <filename unknown>:0
at UIHandler.CreateIndiButton () [0x00000] in <filename unknown>:0
at UIHandler+<RequestAllStudentNames>c__Iterator2.MoveNext () [0x00000] in <filename unknown>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00000] in <filename unknown>:0
I have also added my database to the 'StreamingAssets' folder as shown in the image below:
Below also shows an image of my plugins folder that holds my dll files.

Most tutorials on this topic are outdated.
Looked at the code and found few problems but I can't tell if those are the reason you are getting this error. WWW should be used in a coroutine function so that you can yield or wait for loadDb.isDone is finish by adding yield return null inside the while loop. You can also yield the WWW request itself and that's the method I will use in my answer.
Also, jar:file://" + Application.dataPath is an old code. Use Application.streamingAssetsPath for that. Furthermore, you don't need "URI=file:" + Application.dataPath. Just use Application.persistentDataPath for that.
I will just put an instruction on how to do the setup.
Setting up the MANAGED code part:
1.Go to your Unity installation path
<UnityInstallationDirecory>\Editor\Data\Mono\lib\mono\2.0
Copy the following files:
I18N.MidEast.dll
I18N.Other.dll
I18N.Rare.dll
I18N.West.dll
Mono.Data.Sqlite.dll
Mono.Data.SqliteClient.dll
System.Data.dll
to the project's <ProjectName>\Assets\Plugins path.
This will allow you to compile API from the Mono.Data.Sqlite namespace without any error.
Setting up the UNMANAGED code part:
In this step, you will need to get the native Sqlite library. You can get the source code, build it and use it or use already pre-compiled binray.
1.Get the native library for Windows
Download the pre-compiled sqlite3.dll for Windows 64 bit from here, and put it in the <ProjectName>\Assets\Plugins\x86_64 path.
If using Windows 32 bit then get the sqlite3.dll version from here and put it in the <ProjectName>\Assets\Plugins\x86 path.
2.Get the native library for Android
Download the pre-compiled libsqlite3.so for Android ARM processor from
here and put it in the <ProjectName>\Assets\Plugins\Android\libs\armeabi-v7a path.
Download the pre-compiled libsqlite3.so for Android Intel x86 processor from
here and put it in the <ProjectName>\Assets\Plugins\Android\libs\x86 path.
This covers most processors used on Android devices.
3.Get the native library for UWP
A.Download the WSA folder then put the WSA folder in the <ProjectName>\Assets\Plugins path. That folder contains the native part.
B.Create 2 files named "mcs.rsp" and "csc.rsp" in the <ProjectName>\Assets path.
C.Add the following inside the "mcs.rsp" and "csc.rsp" files:
-r:I18N.MidEast.dll
-r:I18N.Other.dll
-r:I18N.Rare.dll
-r:I18N.West.dll
-r:Mono.Data.Sqlite.dll
-r:Mono.Data.SqliteClient.dll
-r:System.Data.dll
D.You will have to move the managed dlls to the root folder of the project when building for UWP. So, move I18N.MidEast.dll, I18N.Other.dll, I18N.Rare.dll, I18N.West.dll, Mono.Data.Sqlite.dll, Mono.Data.SqliteClient.dll, System.Data.dll to the <ProjectName> path not <ProjectName>\Assets\Plugins path.
4.For iOS, Linux and Mac, it looks like you don't have to download anything else for them or do this step. They usually have the native pre-compiled Sqlite libraries built-in.
Including the Database file in the Build:
1.Create a folder in your <ProjectName>\Assets folder and name it StreamingAssets. Spelling counts and it's case sensitive.
2.Put the database file (TBLDatabase.db) in this StreamingAssets folder.
Accessing the Database File after building the project
Sqlite cannot work on files in the StreamingAssets folder in a build since that's a read-only path. Also, Android requires that you use the WWW API instead of the standard System.IO API to read from the StreamingAssets folder. You have to copy the db file from Application.streamingAssetsPath/filename.db to Application.persistentDataPath/filename.db.
On some platforms, it is required that you create a folder inside Application.persistentDataPath and save data to that folder instead. Always do that. The folder in the example code below is "data" so that will become Application.persistentDataPath/data/filename.db.
3.Because of the statement above, check if the database file exist in the
Application.persistentDataPath/data/filename.db. If it does, use Application.persistentDataPath/data/filename.db as a path for your database operation. If it doesn't, continue from #4.
4.Read and copy the database file from the StreamingAssets folder to Application.persistentDataPath
On some platforms, it is required that you create a folder inside Application.persistentDataPath and save data to that folder instead. Always do that. The folder in the example below is "data".
Detect if this is Android and use WWW read to the file from Application.streamingAssetsPath/filename.db. Use File.ReadAllBytes to read it on anything else other than Android. In your example, you used Application.platform for that. In my example, I will simply check if the path contains "://" or :/// to do that.
5.Once you read the file, write the data you just read to Application.persistentDataPath/data/filename.db with File.WriteAllBytes. Now, you can use this path for your database operation.
6.Prefix "URI=file:" to the Application.persistentDataPath/data/filename.db path and that's the path for that should be used in your database operation with the Sqlite API.
It's very important that you understand all these in order to fix it when something changes but I've already done step #3 to #6 below.
IEnumerator RunDbCode(string fileName)
{
//Where to copy the db to
string dbDestination = Path.Combine(Application.persistentDataPath, "data");
dbDestination = Path.Combine(dbDestination, fileName);
//Check if the File do not exist then copy it
if (!File.Exists(dbDestination))
{
//Where the db file is at
string dbStreamingAsset = Path.Combine(Application.streamingAssetsPath, fileName);
byte[] result;
//Read the File from streamingAssets. Use WWW for Android
if (dbStreamingAsset.Contains("://") || dbStreamingAsset.Contains(":///"))
{
WWW www = new WWW(dbStreamingAsset);
yield return www;
result = www.bytes;
}
else
{
result = File.ReadAllBytes(dbStreamingAsset);
}
Debug.Log("Loaded db file");
//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(dbDestination)))
{
Directory.CreateDirectory(Path.GetDirectoryName(dbDestination));
}
//Copy the data to the persistentDataPath where the database API can freely access the file
File.WriteAllBytes(dbDestination, result);
Debug.Log("Copied db file");
}
try
{
//Tell the db final location for debugging
Debug.Log("DB Path: " + dbDestination.Replace("/", "\\"));
//Add "URI=file:" to the front of the url beore using it with the Sqlite API
dbDestination = "URI=file:" + dbDestination;
//Now you can do the database operation below
//open db connection
var connection = new SqliteConnection(dbDestination);
connection.Open();
var command = connection.CreateCommand();
Debug.Log("Success!");
}
catch (Exception e)
{
Debug.Log("Failed: " + e.Message);
}
}
Usage:
string dbFileName = "TBLDatabase.db";
void Start()
{
StartCoroutine(RunDbCode(dbFileName));
}

This error occurs when we are trying to use SQLite in Unity 2019 or later versions. This error occurs due to missing of Mono library for Unity 2019 or later versions. You won't get this error if you are using Unity 2018 or less versions.
If you installed Unity 2018 or below versions then go to the installation directory <UnityInstallationDirecory>\Editor\Data\Mono\lib\mono\2.0 and copy the following files:
I18N.MidEast.dll
I18N.Other.dll
I18N.Rare.dll
I18N.West.dll
Mono.Data.Sqlite.dll
Mono.Data.SqliteClient.dll
System.Data.dll
to the project's <ProjectName>\Assets\Plugins path.
Or you can simply resolve this error by downgrading the Unity version to 2018 or below.

Related

Getting "NotImplementedException" when attempting Azure connection with Unity (c#)

I'm using Unity 2019.3.6f1. I have created a simple DLL for connecting to Azure and uploading a file. However, when I call this DLL to attemp connecting to the Azure blob storage I get the following stack trace:
NotImplementedException: The method or operation is not implemented.
System.Net.Http.HttpClientHandler.get_MaxConnectionsPerServer () (at
<7ebf3529ba0e4558a5fa1bc982aa8605>:0)
Azure.Core.Pipeline.ServicePointHelpers.SetLimits
(System.Net.Http.HttpMessageHandler messageHandler) (at
:0)
Azure.Core.Pipeline.HttpClientTransport.CreateDefaultClient () (at
:0)
Azure.Core.Pipeline.HttpClientTransport..ctor () (at
:0)
Azure.Core.Pipeline.HttpClientTransport..cctor () (at
:0) Rethrow as
TypeInitializationException: The type initializer for
'Azure.Core.Pipeline.HttpClientTransport' threw an exception.
Azure.Core.ClientOptions..ctor () (at
:0)
Azure.Storage.Blobs.BlobClientOptions..ctor
(Azure.Storage.Blobs.BlobClientOptions+ServiceVersion version) (at
:0)
Azure.Storage.Blobs.BlobServiceClient..ctor (System.String
connectionString, Azure.Storage.Blobs.BlobClientOptions options) (at
:0)
Azure.Storage.Blobs.BlobServiceClient..ctor (System.String
connectionString) (at :0)
Cineon.UnityToAzureConnection+d__1.MoveNext () (at
<5252723c79f94a8cb77bb25e3b9f0396>:0)
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at
<437ba245d8404784b9fbab9b439ac908>:0)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0
(System.Object state) (at <437ba245d8404784b9fbab9b439ac908>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at
:0)
UnityEngine.UnitySynchronizationContext:ExecuteTasks()
Here is my DLL code (in line with this Microsoft quickstart guide):
public class UnityToAzureConnection
{
private readonly static string m_connectionString = "CENSORED";
public static async void CreateConnection(string _path, string _fileName)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(m_connectionString);
BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync("imagetest");
BlobClient blobClient = containerClient.GetBlobClient(_fileName);
using (FileStream _fs = File.Open(Path.Combine(_path, _fileName), FileMode.Open))
{
await blobClient.UploadAsync(_fs, true);
}
}
}
I simply call this DLL's CreateConnection method and pass in my "Application.dataPath" and a filename. However, I then get the above exception.
I have pulled all other needed DLL's from the project build folder into the plugin folder, so am unsure why this is occuring.
Any assistance would be greatly appreciated.
Please note that I am just starting to move out of my comfort zone here, so I'm hoping that my post is to StackOverflow's standard of questions.
[P.S.] Obviously "CENSORED" isn't my actual connection string, just keeping sensitive info hidden.
UPDATE 29/05/2021 I have confirmed that my connection code DOES work when used in a simple .net console app (however, I did have to adjust the containername to be all lowercase). This error only occurs in Unity. Again, any and all help would be appreciated.
As I mentioned here, try it once.
If you have a lower version of Unity, Azure.Core ver. 1.19.0 may be better.
https://forum.unity.com/threads/attempting-to-connec-to-azure-blobs-results-in-notimplementedexception.1117705/
I have experienced the same exception in BlobServiceClient constructor in my mobile project.
The problem was in the version of Azure.Storage.Blobs package. I used the most recent version 12.12.0, and it caused the exception.
When I downgraded Azure.Storage.Blobs package to 12.9.1 all things worked fine.

Can I use MongoDB 3.6 or later with Unity 2018.4?

I'm trying to apply the MongoDB with this unity tutorial MongoDB bought mLab, so the signing into mongoDB is done via mongoDB atlas. And they only support MongoDB 3.6 and later.
I've created a cluster with AWS (similarly to the GUI mLab has),
On "Overview" tab hit the "CONNECT" button. Out of the three options: "Connect with the Mongo Shell" / "Connect Your Application" / "Connect with MongoDB Compass" - Connect Your Application looked the most relevant to the code you've provided.
In: "Connect Your Application" you need to choose a driver: I chose C# / .NET (because this is a C# project), version 2.5 or later.
Got "Connection string": mongodb+srv://user_name:<password>#testmp-pkump.mongodb.net/test?retryWrites=true
Few things:
The project name is not "test" (as it says here, right before the retryWrites = true): mongodb+srv://:#testmp-pkump.mongodb.net/test?retryWrites=true
I've passed this link to the code under: private const string MONGO_URI , and for user name I write the user I've added to the cluster, and for the password, I write the password I've added to the user in the cluster. - and not the user I've used to create the MongoDB account
Running the "Server" scene resolves with an error: =
ArgumentException: Invalid keyword 'mongodb+srv://:#serverclientmp-
1oqao.gcp.mongodb.net/test?retrywrites'.
MongoDB.Driver.MongoConnectionStringBuilder.set_Item (System.String
keyword, System.Object value)
System.Data.Common.DbConnectionStringBuilder.ParseConnectionStringNonOdbc
(System.String connectionString)
System.Data.Common.DbConnectionStringBuilder.ParseConnectionString
(System.String connectionString)
System.Data.Common.DbConnectionStringBuilder.set_ConnectionString
(System.String value) MongoDB.Driver.MongoConnectionStringBuilder..ctor
(System.String connectionString)
MongoDB.Driver.MongoClient.ParseConnectionString (System.String
connectionString) MongoDB.Driver.MongoClient..ctor (System.String
connectionString) Mongo.Init () (at Assets/Scripts/Database/Mongo.cs:16)
Server.Init () (at Assets/Scripts/Server.cs:40) Server.Start () (at
Assets/Scripts/Server.cs:28)
I've tried to change the API Compatibility Level to .NET 4.x and the following error came:
ArgumentException: Invalid option 'retryWrites'.
Parameter name: url
MongoDB.Driver.MongoUrlBuilder.Parse (System.String url) (at
<6da29fc855c44d33ad78b3e27475ff27>:0)
MongoDB.Driver.MongoUrlBuilder..ctor (System.String url) (at
<6da29fc855c44d33ad78b3e27475ff27>:0)
MongoDB.Driver.MongoUrl..ctor (System.String url) (at
<6da29fc855c44d33ad78b3e27475ff27>:0)
MongoDB.Driver.MongoClient.ParseConnectionString (System.String
connectionString) (at <6da29fc855c44d33ad78b3e27475ff27>:0)
MongoDB.Driver.MongoClient..ctor (System.String connectionString) (at
<6da29fc855c44d33ad78b3e27475ff27>:0)
Mongo.Init () (at Assets/Script/Database/Mongo.cs:15)
Server.Init () (at Assets/Script/Server.cs:38)
Server.Start () (at Assets/Script/Server.cs:27)
I would like to know what I need to add to my scripts to make it work or if this tutorial is outdated and a new approach needs to be done.
Thanks
I know I am 8 months too late but, I have recently began working with MongoDB in Unity and I don't know if this will help but the short answer is I'm not sure. But I managed to connect to my remote and local db, so I'll explain how I did it below.
The Long answer:
I did not watch the tutorial video you followed but i setup my connections with these dll files (I am assuming you also created a Plugins folder for them). As for the "tutorial", I simply followed this quick start guide and this one. Everything else API related I just read the documentation.
I'm using MongoDB v4.2.1
I'm using Unity 2019.3
I'm using API compatibility .Net Standard 2.0
I'm using the admin user on the cluster. I don't know what your case is but I remember the documentation says that your user should have read and write privileges, so check that maybe.
Finally, I had an error when I first tried to connect to the remote DB, but maybe because I have my code wrapped around a try catch block, it didn't produce the error you provided but rather this:
Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1 mongo c# driver
Which occurred because I forgot to take the angle brackets out of the code and had this instead - <password> - yes I left my password wrapped around the brackets, rookie mistake lol.
Here's how I connected to my Atlas cluster:
private const string remoteDB = "mongodb+srv://user_name:<password>#mycluster.mongodb.net/test?retryWrites=true";
static MongoClient newClient = new MongoClient(remoteDB);
internal Database()//This is a constructor, I'm not using MonoBehaviour
{
db = newClient.GetDatabase(DATABASE_NAME);
lbCollection = db.GetCollection<BsonDocument>(lbColl);
gameStateCollection = db.GetCollection<BsonDocument>(gameStateColl);
}
internal static async Task TestConnection()
{
try
{
await GetDBNames();
}
catch (Exception e)
{
Debug.Log("<color=red> Error</color> found while testing db connection:");
Debug.Log(e.Message);
}
}
private static async Task<List<string>> GetDBNames()
{
List<string> dbNames = new List<string>();
var dbs = await newClient.ListDatabaseNamesAsync();
dbNames = dbs.ToList();
if (dbNames.Count > 0)
{
Debug.Log($"Databases in {nameof(dbNames)}: ");
for (int i = 0; i < dbNames.Count; i++)
{
Debug.Log($"{dbNames[i]}");
}
return dbNames;
}
Debug.Log($"<color=red>{nameof(GetDBNames)}</color> Returning null. No Dbs found.");
return null;
}

Firebase initialisation exception with Unity Android

We are developing an application in unity with the aim to deploy to android and are using firebase to facilitate user login and real time database.
We are using the real time database to store the location and information about specific objects and synchronize them across all players. The database also stores the individual data about each user. Our implementation has a single parent object called GenericInterface which has all of the shared code between the PlayerInterface and BuildingInterface which extend GenericInterface.
GenericInterface has the initialization code for our firebase implemention:
Auth = FirebaseAuth.DefaultInstance;
// Set this before calling into the realtime database.
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(/*DB Url*/);
//set up restricted access.
// Set these values before calling into the realtime database.
FirebaseApp.DefaultInstance.SetEditorP12FileName(/* P12 filename */);
FirebaseApp.DefaultInstance.SetEditorServiceAccountEmail(/*AccountEmail*/);
FirebaseApp.DefaultInstance.SetEditorP12Password(/*Password*/);
// Get the root reference location of the database
Reference = FirebaseDatabase.DefaultInstance.RootReference;
This Reference is then used by PlayerInterface and BuildingInterface to load data through listeners, for example in the PlayerInterface:
var path = string.Format("players/{0}", pid);
Reference = FirebaseDatabase.DefaultInstance.GetReference(path);
Reference.ValueChanged += HandleValueChanged;
Where pid is the player's Id.
The project (apart from player login) works as intended when launched and run in the unity editor with buildings being fetched and displayed correctly by the BuildingInterface. However when building and running on either an AVD or Android Phone, none are displayed and a adb.exe logcat -s Unity shows the following error:
DBInterfaces.Abstract.GenericInterface`1:_start() (at [...]\Assets\Scripts\Firebase\Interfaces\Abstract\GenericInterface.cs:24)
DBInterfaces.Abstract.GenericInterface`1:.ctor() (at [...]\Assets\Scripts\Firebase\Interfaces\Abstract\GenericInterface.cs:18)
DBInterfaces.BuildingInterface:.ctor() (at E:\Users\Documen
InitializationException: Exception of type 'Firebase.InitializationException' was thrown.
at Firebase.Auth.FirebaseAuth+<GetAuth>c__AnonStorey1.<>m__0 () [0x00000] in <filename unknown>:0
at Firebase.FirebaseApp.TranslateDllNotFoundException (System.Action closureToExecute) [0x00000] in <filename unknown>:0
[Trimmed long file path for clarity]
Line 24 of GenericInterface Corresponds to Auth = FirebaseAuth.DefaultInstance;
We have been working on this for the better part of the week and haven't found any potential solutions.

Can't write input to process C# Mono

I'm having a issue trying to write input to linux process. Here the code goes:
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WorkingDirectory = "'/home/"+user+"/pacotes/"+nome_pacote.Text+"-1.0/'";
process.StartInfo.FileName="dh_make";
process.StartInfo.Arguments="-n -s";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.Start();
Thread.Sleep(3000);
process.StandardInput.WriteLine();
And here goes the error:
System.IO.IOException: Write fault on path /home/vinholi/Ubuntu One/Uso do linux em flash drives/Programa gerador de .deb/GeradorDeb/GeradorDeb/bin/Debug/[Unknown]
at System.IO.FileStream.FlushBuffer (System.IO.Stream st) [0x00000] in :0
at System.IO.FileStream.FlushBuffer () [0x00000] in :0
at System.IO.FileStream.Dispose (Boolean disposing) [0x00000] in :0
One likely explanation for this error is that the process exited before you're attempting to write to it. I tried this with /bin/date and a legitimate StartInfo.WorkingDirectory and the assembly is /Workspace/export/misc/H.exe. This yields:
Unhandled Exception:
System.IO.IOException: Write fault on path /Workspace/export/misc/[Unknown]
at System.IO.FileStream.WriteInternal (System.Byte[] src, Int32 offset, Int32 count) [0x00097] in /Workspace/mono/mcs/class/corlib/System.IO/FileStream.cs:658
at System.IO.FileStream.Write (System.Byte[] array, Int32 offset, Int32 count) [0x000aa] in /Workspace/mono/mcs/class/corlib/System.IO/FileStream.cs:634
at System.IO.StreamWriter.FlushBytes () [0x00043] in /Workspace/mono/mcs/class/corlib/System.IO/StreamWriter.cs:222
at System.IO.StreamWriter.FlushCore () [0x00012] in /Workspace/mono/mcs/class/corlib/System.IO/StreamWriter.cs:203
at System.IO.StreamWriter.Write (System.Char[] buffer) [0x00022] in /Workspace/mono/mcs/class/corlib/System.IO/StreamWriter.cs:351
at System.IO.TextWriter.WriteLine () [0x00000] in /Workspace/mono/mcs/class/corlib/System.IO/TextWriter.cs:257
at X.Main () [0x00066] in /Workspace/export/misc/H.cs:17
You are getting this every time when you're using an invalid directory in process.StartInfo.WorkingDirectory. There's nothing that can be done about that, though the error message should be made clearer.
Your directory is invalid because of the quotation marks. You also should not concatenate path names the way you did as it makes your application non-portable to non-Unix operating systems. Instead, write it as:
var home = Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
process.StartInfo.WorkingDirectory = Path.Combine (home, "pacotes", nome_pacote.Text+"-1.0");
Using Environment.GetFolderPath() makes it easier to run your application on a Mac (where home directories are in /Users/<username> instead of /home/<username>).
One other possibility is that the binary that you're launching is a dangling symlink. Check if the binary exists or is a symlink pointing to a valid binary.
As another response stated "One likely explanation for this error is that the process exited before you're attempting to write to it."
And that was the problem I had -- even after setting the WorkingDirectory as described in the other answer. And in your same situation: C#, Mono, Ubuntu.
Ultimately the solution in my case was to set the Arguments so that the process was not exiting, and was really trying to read input from the StandardInput.
The path you are trying to write too can't be written too.
" /home/vinholi/Ubuntu One/Uso do linux em flash drives/Programa gerador de .deb/GeradorDeb/GeradorDeb/bin/Debug/"
Looks like its a usb drive. Check the following:
Its not readonly.
Its not been mounted readonly.
The usb drive is not out of space.

MonoTouch iOS application can't find files in directory on device - works on simulator

If I run this code on the device, it returns "got it":
if (Directory.Exists (NSBundle.MainBundle.BundlePath + "/ARCSDev")) {
Console.WriteLine ("got it");
} else {
Console.WriteLine ("can't find it");
}
Which means the directory is in the main bundle.
I need to use that file in this method call:
private void generateChart (int chartNumber, string palette)
{
string filePath = NSBundle.MainBundle.BundlePath + "/ARCSDev";
Console.WriteLine("Filepath - " + filePath);
Loader loader = new Loader (filePath);
loader.LoadChart (Convert.ToString (chartNumber));
The above code works fine on the simulator, but not on the device.
I get the following stack trace when the error occurs on the device:
System.InvalidOperationException: Operation is not valid due to the current state of the object
at System.Linq.Enumerable.Max[TileIndexRecord] (IEnumerable`1 source, System.Func`2 selector) [0x00000] in <filename unknown>:0
at MyCompany.Data.Arcs.Loader.ExtractWriteableBitmap (MyCompany.Data.Arcs.Records.RGBPaletteRecord rgbPalette, Double dpi, MyCompany.Data.Arcs.Raschts.ChartIndexFile indexFile, MyCompany.Data.Arcs.Raschts.RasterChartFile chartFile) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Loader.cs:571
at MyCompany.Data.Arcs.Loader.GetHiResImage (MyCompany.Data.Arcs.Records.RGBPaletteRecord rgbPalette) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Loader.cs:362
at ARCSViewer.SecondViewController.generateChart (Int32 chartNumber, System.String palette) [0x0004e] in /Users/me/Desktop/ARCSViewer/ARCSViewer/SecondViewController.cs:118
at ARCSViewer.SecondViewController.ViewDidAppear (Boolean animated) [0x00007] in /Users/me/Desktop/ARCSViewer/ARCSViewer/SecondViewController.cs:84
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at ARCSViewer.Application.Main (System.String[] args) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Main.cs:17
The directory contains unix executable files and radiance files.
Can anyone explain whats going on? The file is definitely in the right place (the bundle) as I've tested the code that checks for existence with other files that I know to be there as well..
It might be an issue of case sensitivity, the simulator is case-insensitive while the device is case sensitive. Check your files to see if you're accessing all of them with the right case (i.e. not only the directory).
The permissions on the devices are very different (far more restricted) from the simulator. There's a lot of place you do not have access for various reasons (e.g. changing some files would break the application digital signature which would disallow your application from running anymore).
Also if your application does not follow Apple guidelines on where (and when) to store data your application will be rejected (if you target the appstore).
There's a nice article from Xamarin on how to work with the iOS file system.
I solved the problem, several parts of my application use code like below:
fs = new FileStream(fileName, FileMode.Open);
I needed to change every occurrence of FileStream() to the following:
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
As my files are in the bundle and don't need to make use of write operations, this limits the file stream to only read operations and allowed the files to be read in properly.

Categories

Resources