Binary edit a file from data folder

magic

Active member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Mar 5, 2018
Messages
252
Hi,

I try to edit a file in data folder with
Code:
Local file:=FileStream.Open( "cerberus://data/test.bin","r" )
Local i:=file.ReadInt()
DrawText i,100,100
file.Close
The stream seem Null. I check the file is there. m I missing anything?
Btw.. I'm in android
 
Seems FileStream.Open can only access cerberus://internal/ - see here

Apparently you're supposed to use DataBuffer.Load instead.
 
Oh thank you for the info.
Can we save the DataBuffer to a file?
 
Oh thank you for the info.
Can we save the DataBuffer to a file?
Use a FileStream . And with it, look into its Write methods.
 
Did something change from Monkey? Because I had code written in Monkey that would read a file from the data folder...

I just did a
Code:
fs = FileStream.Open("data/" + file,"r")
and I know it worked on Monkey, because I have an old APK from this same project that still works (I'm loading a map)

Moved the file to Cerberus, compile it to Android and it doesn't anymore...?! (It is running on my own target, old from mojo1)
 
FileStream.Open("data/" + file,"r")
may be that "data/" is change. I think something cerberus://blabla/ .. I can't remember. You can check doc
 
I've tried cerberus://data/ no change. tried ./assets/cerberus/ and nothing.
 
I can look into this tomorrow. @SLotman Do you know the monkey version you built it with?
I can remember some changes/bug fixes regarding fixPath() but nothing that should break stuff ;)
 
Yeah, another project with a JSON reader I have here is failing too. Weird that reading and writeing from internal:// works, but not reading from cerberus://data/ ?!
 
I was trying the old monkey version I have - and surprise - its failiing on v87 as well.

Weird that I'm looking at old sources, and PathToFilePath is exactly the same as the one in Cerberus - so thats not the culprit :(
 
Last edited:
There is something really weird going on. I just altered another project on Monkey that uses JsonParser...works on Monkey 87, on Cerberus its throwing 'invalid Json' - maybe some android permission thing needed?
 
Well Cerberus is definitely using a newer Android Api which introduces more and more finegrained permissions often with runtime popups.
 
Hmmm...

I think this is the problem: Android does not permit to use RandomAcessFile outside of the internal storage, and thats exactly what Cerberus is doing when the filenotfound exception is raised:

Code:
    public RandomAccessFile OpenFile( String path,String mode ){
        try{
            return new RandomAccessFile( PathToFilePath( path ),mode );
        }catch( IOException ex ){
        }
        return null;
    }

I really don't know if there is a solution to this, other than the nasty one presented as to "copy" the desired file to internal and then read it from there :/
 
Yes hmmm...
I had a search in the old monkey repo and found that in 2014 there was a commit that saved asset files to an external folder and also had a PathToFilePath() that returned PathToAssetPath()
But this was ditched before the OpenSource release of Monkey. I am not sure we should incorporate that hack again ...
 
The 'PathToAssetPath' function seems to be in the android target, even in Cerberus...! The only difference is the last line, of PathToFilePath returning
Code:
return PathToAssetPath(path);
instead of
Code:
return ""

I've made the change here, but it didn't help, still returning null to filestream.open() :(
 
I was referring to a larger commit (April 2014) that also includes the functionality of copying the asset file to a external location first. The PathToAssetPath() function on its own doesn't solve anything as it just removes some clutter in the string.

That's what I found out about the issue so far:
  • The reason random access for the data folder is not supported by android is that all asset files are compressed inside the apk (not as simple files in a folder) and therefore only accessible for sequential access.
  • So if we need real RandomAccessFile functionality on an asset file we have to copy it, which means extracting it to a different location. That seems to be the only way of having real random access performance.
  • There might be a direct way to get the functionality of random access while still using the asset file itself for reading data by using the AssetFileDescriptor for getting to a certain location in a file but it still sits on the sequential loading process and leads to performance issues. - I am not sure if this would be an option at all!
All in all I tend to say I would implement that copy-to-internal functionality with a warning of that extracting process happening in the background.
 
I ended up replacing it with a databuffer in my code. But if there was a way to just read it sequentially (like a fileStream, without seek, just readbyte, readint, readstring, etc) - I think that could be a good compromise (and something better than databuffer, specially when its a large file and you can't read it all into memory)
 
But if there was a way to just read it sequentially (like a fileStream, without seek, just readbyte, readint, readstring, etc) - I think that could be a good compromise
That's a good idea! I will see if I can get a poor mans InputFileStream in CX running that can use most of the functionality of CX FileStream, probably except seek() and eof() and certainly all write commands.
 
@SLotman I made a FileInputStream Class as a module for CX. At the moment it only supports Android and Desktop for now and on Desktop it is just a cheap copy of the regular FileStream restricted to read access. It's in this branch on Github until it gets merged.
 
Back
Top Bottom