Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File i/o broken on Android #242

Open
Claude-Rains opened this issue Nov 14, 2014 · 2 comments
Open

File i/o broken on Android #242

Claude-Rains opened this issue Nov 14, 2014 · 2 comments

Comments

@Claude-Rains
Copy link

I've been having problems saving and loading data on Android. All of the methods that can be used on PC (Export/Exec, TAMLWrite/TAMLRead, OpenForWrite/OpenForRead) fail on Android. The first one I used, which I use in the PC version of my app, was to export the data I want to save, in this case a High Score, as a pref in a cs file, and then execute this file, if found, the next time the game is run. The file seems to be created just fine, but when the game is run the second time and the file is read, it crashes. It was suggested on the forums that I try creating and exporting a file object using TAMLWrite and TAMLRead. This didn't crash the game, but threw errors to the console and didn't give me my high score data. I then tried using the basic functions for saving and loading data, OpenForWrite, and OpenForRead. It was these functions I chose to focus on when I sought help on the irc. A user named "az" walked me through debugging these functions which is where we found the break. The following are the script functions I'm currently trying to use to save data on Android. Note that I still use a pref internally to track the score, all I've changed are the methods that save and retrieve that information:

    if ( isFile(getUserDataDirectory() @ "/scores.txt") )
        {
        %file = new FileObject();
        %didopen = %file.OpenForRead(getUserDataDirectory() @ "/scores.txt");
        echo("got this far? "@ %didopen);
        while( !%file.isEOF() )
        {
            %line = %file.readline();
            echo(%line);
        }
        $Pref::Game::HighScore = %line;
        %file.close();
        }

This is part of the AppCore::create function run at the start of the game.The first echo reports if the OpenForRead function was called successfully, while the second reports the data that was read.

This is what I use to save the score. This appears to be working correctly (I have verified with a rooted file browser that the file is being created and contains exactly what it's supposed to), but I will include it anyway:

        %file = new FileObject();
        %file.OpenForWrite(getUserDataDirectory() @ "/scores.txt");
        %file.writeline($Pref::Game::HighScore);
        %file.close();

All I'm writing is a number.

This is the function in fileObject.cc that I modified at az's suggestion:

bool FileObject::readMemory(const char *fileName)
{
   StringTableEntry fileToOpen = NULL;

   char buffer[1024];

   Con::expandPath( buffer, sizeof( buffer ), fileName );

   fileToOpen = StringTable->insert(buffer);

   close();
   Stream *s = ResourceManager->openStream(fileToOpen);
   if(!s)
      return false;
   mBufferSize = ResourceManager->getSize(fileToOpen);
   mFileBuffer = (U8 *) dMalloc(mBufferSize + 1);
   mFileBuffer[mBufferSize] = 0;
   s->read(mBufferSize, mFileBuffer);
   Con::errorf("mFileBuffer=%s, mBufferSize=%i",mFileBuffer, mBufferSize);
   ResourceManager->closeStream(s);
   mCurPos = 0;

   return true;
}

As you can see, the only change is the addition of single line that reports the contents and size of the file buffer. Earlier I had one reporting the path, but this appeared to be correct, so I left it out.

The following is a record of LogCat for two consecutive runs of the game, after an initial run to create the high score file. The first score that was read was 4 digits long, the second was 3 digits:

11-14 01:16:57.085 I/ActivityManager(23858): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity} from pid 2270
11-14 01:16:57.265 I/ActivityManager(23858): Start proc com.edfgames.missilecommander for activity com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity: pid=2666 uid=10134 gids={50134, 3003, 1028, 1015}
11-14 01:16:58.854 I/ActivityManager(23858): Displayed com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity: +1s608ms
11-14 01:16:58.854 I/Timeline(23858): Timeline: Activity_windows_visible id: ActivityRecord{42fcd5f8 u0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity t41} time:55382158
11-14 01:16:59.081 I/Torque2D( 2666): [2014/11/14 01:16:59] Input Init:
11-14 01:16:59.081 I/Torque2D( 2666): [2014/11/14 01:16:59] []Input::enable.
11-14 01:16:59.081 I/Torque2D( 2666): [2014/11/14 01:16:59] 
11-14 01:16:59.081 I/Torque2D( 2666): [2014/11/14 01:16:59] Video Init:
11-14 01:16:59.089 I/Torque2D( 2666): [2014/11/14 01:16:59] --------------------------------------------------------------------------------
11-14 01:16:59.093 I/Torque2D( 2666): [2014/11/14 01:16:59] Video initialization:
11-14 01:16:59.093 I/Torque2D( 2666): [2014/11/14 01:16:59]    Accelerated OpenGL display device detected.
11-14 01:16:59.093 I/Torque2D( 2666): [2014/11/14 01:16:59] 
11-14 01:17:10.733 I/torque2d( 2666): time in java: 11640.0
11-14 01:17:10.753 I/Torque2D( 2666): [2014/11/14 01:17:10] Console trace is off.
11-14 01:17:10.776 I/torque2d( 2666): time in dir java: 25.0
11-14 01:17:10.796 I/Torque2D( 2666): [2014/11/14 01:17:10] mFileBuffer=8%�R�A, mBufferSize=6
11-14 01:17:10.796 I/Torque2D( 2666): [2014/11/14 01:17:10] got this far? 1
11-14 01:17:10.796 I/Torque2D( 2666): [2014/11/14 01:17:10] 8%�R�A
11-14 01:17:10.796 I/Torque2D( 2666): [2014/11/14 01:17:10] Default BPP Chosen , $pref::Android::ScreenDepth was not found.
11-14 01:17:10.796 I/Torque2D( 2666): [2014/11/14 01:17:10] Activating the OpenGL display device...
11-14 01:17:10.796 I/Torque2D( 2666): [2014/11/14 01:17:10]  OpenGLDevice activating...
11-14 01:17:10.800 I/Torque2D( 2666): [2014/11/14 01:17:10] OpenGL driver information:
11-14 01:17:10.800 I/Torque2D( 2666): [2014/11/14 01:17:10]   Vendor: Imagination Technologies
11-14 01:17:10.800 I/Torque2D( 2666): [2014/11/14 01:17:10]   Renderer: PowerVR SGX 540
11-14 01:17:10.800 I/Torque2D( 2666): [2014/11/14 01:17:10]   Version: OpenGL ES-CM 1.1
11-14 01:17:10.800 I/Torque2D( 2666): [2014/11/14 01:17:10] OpenGL Init: Enabled Extensions
11-14 01:17:10.800 I/Torque2D( 2666): [2014/11/14 01:17:10]   Vertical Sync
11-14 01:17:10.800 I/Torque2D( 2666): [2014/11/14 01:17:10]   ATI_FSAA
11-14 01:17:10.800 I/Torque2D( 2666): [2014/11/14 01:17:10] OpenGL Init: Disabled Extensions
11-14 01:17:10.804 I/Torque2D( 2666): [2014/11/14 01:17:10]   ARB_multitexture
11-14 01:17:10.804 I/Torque2D( 2666): [2014/11/14 01:17:10]   EXT_blend_color
11-14 01:17:10.804 I/Torque2D( 2666): [2014/11/14 01:17:10]   EXT_blend_minmax
11-14 01:17:10.804 I/Torque2D( 2666): [2014/11/14 01:17:10]   EXT_paletted_texture
11-14 01:17:10.804 I/Torque2D( 2666): [2014/11/14 01:17:10]   EXT_compiled_vertex_array
11-14 01:17:10.804 I/Torque2D( 2666): [2014/11/14 01:17:10]   NV_vertex_array_range
11-14 01:17:10.804 I/Torque2D( 2666): [2014/11/14 01:17:10]   EXT_texture_env_combine
11-14 01:17:10.808 I/Torque2D( 2666): [2014/11/14 01:17:10]   EXT_packed_pixels
11-14 01:17:10.808 I/Torque2D( 2666): [2014/11/14 01:17:10]   EXT_fog_coord
11-14 01:17:10.808 I/Torque2D( 2666): [2014/11/14 01:17:10]   ARB_texture_compression
11-14 01:17:10.808 I/Torque2D( 2666): [2014/11/14 01:17:10]   EXT_texture_compression_s3tc
11-14 01:17:10.808 I/Torque2D( 2666): [2014/11/14 01:17:10]   3DFX_texture_compression_FXT1
11-14 01:17:10.808 I/Torque2D( 2666): [2014/11/14 01:17:10]   (ARB|EXT)_texture_env_add
11-14 01:17:10.808 I/Torque2D( 2666): [2014/11/14 01:17:10]   EXT_texture_filter_anisotropic
11-14 01:17:10.812 I/Torque2D( 2666): [2014/11/14 01:17:10] 
11-14 01:17:10.812 I/Torque2D( 2666): [2014/11/14 01:17:10]  set screen mode 800 x 480 x 32, fullscreen, force it, dont repaint
11-14 01:17:10.812 I/Torque2D( 2666): [2014/11/14 01:17:10] Cleaning up the display device...
11-14 01:17:10.812 I/Torque2D( 2666): [2014/11/14 01:17:10] >> Attempting to change display settings to fullscreen 800x480x32...
11-14 01:17:10.812 I/Torque2D( 2666): [2014/11/14 01:17:10] Resurrecting the texture manager...
11-14 01:17:10.812 I/Torque2D( 2666): [2014/11/14 01:17:10] Max Texture Size reported as: 2048
11-14 01:17:10.815 I/Torque2D( 2666): [2014/11/14 01:17:10]  set screen mode 800 x 480 x 32, windowed, dont force it, repaint
11-14 01:17:10.815 I/Torque2D( 2666): [2014/11/14 01:17:10] Cleaning up the display device...
11-14 01:17:10.815 I/Torque2D( 2666): [2014/11/14 01:17:10] >> Attempting to change display settings to windowed 800x480x32...
11-14 01:17:10.815 I/Torque2D( 2666): [2014/11/14 01:17:10] Resurrecting the texture manager...
11-14 01:17:10.831 I/Torque2D( 2666): [2014/11/14 01:17:10] OpenAL Driver Init
11-14 01:17:10.847 I/Torque2D( 2666): [2014/11/14 01:17:10] OpenAL Driver Init Success
11-14 01:17:10.960 I/Torque2D( 2666): [2014/11/14 01:17:10] @@@ Console::create function called
11-14 01:17:17.351 I/InputDispatcher(23858): Window 'Window{42b7a4d0 u0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity}' spent 2423.6ms processing the last input event: MotionEvent(deviceId=9, source=0x00001002, action=1, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.3, yPrecision=2.1, displayId=0, pointers=[0: (365.6, 227.3)]), policyFlags=0x47000000
11-14 01:17:18.354 I/Torque2D( 2666): [2014/11/14 01:17:18] GuiSpriteCtrl::onWake() - No Image or Animation Asset defined.
11-14 01:17:42.276 I/Torque2D( 2666): [2014/11/14 01:17:42] GuiSpriteCtrl::onWake() - No Image or Animation Asset defined.
11-14 01:17:48.058 I/Torque2D( 2666): [2014/11/14 01:17:48] GuiSpriteCtrl::onWake() - No Image or Animation Asset defined.
11-14 01:18:47.483 I/Torque2D( 2666): [2014/11/14 01:18:47] GuiSpriteCtrl::onWake() - No Image or Animation Asset defined.
11-14 01:18:48.382 I/Torque2D( 2666): [2014/11/14 01:18:48] @@@ Console::destroy function called
11-14 01:18:48.440 I/Torque2D( 2666): [2014/11/14 01:18:48] Shutting down the OpenGL display device...
11-14 01:18:48.440 I/Torque2D( 2666): [2014/11/14 01:18:48] []Input::disable.
11-14 01:18:48.749 W/ActivityManager(23858): Force removing ActivityRecord{42fcd5f8 u0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity t41}: app died, no saved state
11-14 01:18:48.765 I/WindowState(23858): WIN DEATH: Window{42b7a4d0 u0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity}
11-14 01:18:48.769 W/InputDispatcher(23858): channel '42b7a4d0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
11-14 01:18:48.769 E/InputDispatcher(23858): channel '42b7a4d0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
11-14 01:18:48.769 W/InputDispatcher(23858): Attempted to unregister already unregistered input channel '42b7a4d0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity (server)'
11-14 01:18:52.147 I/ActivityManager(23858): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity} from pid 2270
11-14 01:18:52.343 I/ActivityManager(23858): Start proc com.edfgames.missilecommander for activity com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity: pid=2687 uid=10134 gids={50134, 3003, 1028, 1015}
11-14 01:18:53.952 I/ActivityManager(23858): Displayed com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity: +1s644ms
11-14 01:18:53.952 I/Timeline(23858): Timeline: Activity_windows_visible id: ActivityRecord{428fe708 u0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity t42} time:55497256
11-14 01:18:54.175 I/Torque2D( 2687): [2014/11/14 01:18:54] Input Init:
11-14 01:18:54.175 I/Torque2D( 2687): [2014/11/14 01:18:54] []Input::enable.
11-14 01:18:54.179 I/Torque2D( 2687): [2014/11/14 01:18:54] 
11-14 01:18:54.183 I/Torque2D( 2687): [2014/11/14 01:18:54] Video Init:
11-14 01:18:54.183 I/Torque2D( 2687): [2014/11/14 01:18:54] --------------------------------------------------------------------------------
11-14 01:18:54.187 I/Torque2D( 2687): [2014/11/14 01:18:54] Video initialization:
11-14 01:18:54.187 I/Torque2D( 2687): [2014/11/14 01:18:54]    Accelerated OpenGL display device detected.
11-14 01:18:54.187 I/Torque2D( 2687): [2014/11/14 01:18:54] 
11-14 01:19:05.569 I/torque2d( 2687): time in java: 11385.0
11-14 01:19:05.589 I/Torque2D( 2687): [2014/11/14 01:19:05] Console trace is off.
11-14 01:19:05.612 I/torque2d( 2687): time in dir java: 24.0
11-14 01:19:05.632 I/Torque2D( 2687): [2014/11/14 01:19:05] mFileBuffer=���R�, mBufferSize=5
11-14 01:19:05.632 I/Torque2D( 2687): [2014/11/14 01:19:05] got this far? 1
11-14 01:19:05.632 I/Torque2D( 2687): [2014/11/14 01:19:05] ���R�
11-14 01:19:05.636 I/Torque2D( 2687): [2014/11/14 01:19:05] Default BPP Chosen , $pref::Android::ScreenDepth was not found.
11-14 01:19:05.636 I/Torque2D( 2687): [2014/11/14 01:19:05] Activating the OpenGL display device...
11-14 01:19:05.636 I/Torque2D( 2687): [2014/11/14 01:19:05]  OpenGLDevice activating...
11-14 01:19:05.636 I/Torque2D( 2687): [2014/11/14 01:19:05] OpenGL driver information:
11-14 01:19:05.640 I/Torque2D( 2687): [2014/11/14 01:19:05]   Vendor: Imagination Technologies
11-14 01:19:05.640 I/Torque2D( 2687): [2014/11/14 01:19:05]   Renderer: PowerVR SGX 540
11-14 01:19:05.640 I/Torque2D( 2687): [2014/11/14 01:19:05]   Version: OpenGL ES-CM 1.1
11-14 01:19:05.640 I/Torque2D( 2687): [2014/11/14 01:19:05] OpenGL Init: Enabled Extensions
11-14 01:19:05.640 I/Torque2D( 2687): [2014/11/14 01:19:05]   Vertical Sync
11-14 01:19:05.640 I/Torque2D( 2687): [2014/11/14 01:19:05]   ATI_FSAA
11-14 01:19:05.640 I/Torque2D( 2687): [2014/11/14 01:19:05] OpenGL Init: Disabled Extensions
11-14 01:19:05.644 I/Torque2D( 2687): [2014/11/14 01:19:05]   ARB_multitexture
11-14 01:19:05.644 I/Torque2D( 2687): [2014/11/14 01:19:05]   EXT_blend_color
11-14 01:19:05.644 I/Torque2D( 2687): [2014/11/14 01:19:05]   EXT_blend_minmax
11-14 01:19:05.644 I/Torque2D( 2687): [2014/11/14 01:19:05]   EXT_paletted_texture
11-14 01:19:05.644 I/Torque2D( 2687): [2014/11/14 01:19:05]   EXT_compiled_vertex_array
11-14 01:19:05.644 I/Torque2D( 2687): [2014/11/14 01:19:05]   NV_vertex_array_range
11-14 01:19:05.644 I/Torque2D( 2687): [2014/11/14 01:19:05]   EXT_texture_env_combine
11-14 01:19:05.644 I/Torque2D( 2687): [2014/11/14 01:19:05]   EXT_packed_pixels
11-14 01:19:05.644 I/Torque2D( 2687): [2014/11/14 01:19:05]   EXT_fog_coord
11-14 01:19:05.647 I/Torque2D( 2687): [2014/11/14 01:19:05]   ARB_texture_compression
11-14 01:19:05.647 I/Torque2D( 2687): [2014/11/14 01:19:05]   EXT_texture_compression_s3tc
11-14 01:19:05.647 I/Torque2D( 2687): [2014/11/14 01:19:05]   3DFX_texture_compression_FXT1
11-14 01:19:05.647 I/Torque2D( 2687): [2014/11/14 01:19:05]   (ARB|EXT)_texture_env_add
11-14 01:19:05.647 I/Torque2D( 2687): [2014/11/14 01:19:05]   EXT_texture_filter_anisotropic
11-14 01:19:05.647 I/Torque2D( 2687): [2014/11/14 01:19:05] 
11-14 01:19:05.647 I/Torque2D( 2687): [2014/11/14 01:19:05]  set screen mode 800 x 480 x 32, fullscreen, force it, dont repaint
11-14 01:19:05.647 I/Torque2D( 2687): [2014/11/14 01:19:05] Cleaning up the display device...
11-14 01:19:05.651 I/Torque2D( 2687): [2014/11/14 01:19:05] >> Attempting to change display settings to fullscreen 800x480x32...
11-14 01:19:05.651 I/Torque2D( 2687): [2014/11/14 01:19:05] Resurrecting the texture manager...
11-14 01:19:05.651 I/Torque2D( 2687): [2014/11/14 01:19:05] Max Texture Size reported as: 2048
11-14 01:19:05.651 I/Torque2D( 2687): [2014/11/14 01:19:05]  set screen mode 800 x 480 x 32, windowed, dont force it, repaint
11-14 01:19:05.651 I/Torque2D( 2687): [2014/11/14 01:19:05] Cleaning up the display device...
11-14 01:19:05.651 I/Torque2D( 2687): [2014/11/14 01:19:05] >> Attempting to change display settings to windowed 800x480x32...
11-14 01:19:05.655 I/Torque2D( 2687): [2014/11/14 01:19:05] Resurrecting the texture manager...
11-14 01:19:05.663 I/Torque2D( 2687): [2014/11/14 01:19:05] OpenAL Driver Init
11-14 01:19:05.690 I/Torque2D( 2687): [2014/11/14 01:19:05] OpenAL Driver Init Success
11-14 01:19:05.800 I/Torque2D( 2687): [2014/11/14 01:19:05] @@@ Console::create function called
11-14 01:19:10.612 I/InputDispatcher(23858): Window 'Window{424a5008 u0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity}' spent 2328.0ms processing the last input event: MotionEvent(deviceId=9, source=0x00001002, action=1, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.3, yPrecision=2.1, displayId=0, pointers=[0: (485.9, 240.0)]), policyFlags=0x47000000
11-14 01:19:11.636 I/Torque2D( 2687): [2014/11/14 01:19:11] GuiSpriteCtrl::onWake() - No Image or Animation Asset defined.
11-14 01:19:17.597 I/Torque2D( 2687): [2014/11/14 01:19:17] @@@ Console::destroy function called
11-14 01:19:17.636 I/Torque2D( 2687): [2014/11/14 01:19:17] Shutting down the OpenGL display device...
11-14 01:19:17.636 I/Torque2D( 2687): [2014/11/14 01:19:17] []Input::disable.
11-14 01:19:17.897 I/WindowState(23858): WIN DEATH: Window{424a5008 u0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity}
11-14 01:19:17.901 W/ActivityManager(23858): Force removing ActivityRecord{428fe708 u0 com.edfgames.missilecommander/com.garagegames.torque2d.MyNativeActivity t42}: app died, no saved state

As you can see, the data in the FileBuffer is incorrect. I did a few more runs reading the same text file and got a different set of seemingly random characters in the FileBuffer each time. az suggested this meant sections of RAM were being read instead of my data.

I am not a programmer, this guided debug was literally the first time I ever touched C++ code, so I can't go any farther with this. I'm hoping someone here who's more experienced with Android and the i/o functions will be able to track this down with the information I've provided.

@kynora
Copy link
Contributor

kynora commented Nov 30, 2015

Android not allow to write to app directory but can write to cache directory.
I have solution to write file to cache directory, you can try it.
This is link to zip file :
https://drive.google.com/file/d/0B3TDPjv2bbg-clhPS3JodDVKbmc/view?usp=sharing

@greenfire27
Copy link
Contributor

With that latest pull request this should be fixed. Please let me know if problem is still there or not using the latest from the dev branch. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants