Skip to content

Commit

Permalink
RecordStore: Add more ID checks, and save LastModified value
Browse files Browse the repository at this point in the history
Fixes Wakeboard Unleashed (which also uses Motorola FunLights) and
a few others.

Since lastModified should actually be part of the rms header, but
it was not saved before, it now has to be saved at the end of the
file, after the records. Otherwise it would render older saves
incompatible.
  • Loading branch information
AShiningRay committed Dec 19, 2024
1 parent cd68c5f commit 661761d
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions src/javax/microedition/rms/RecordStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class RecordStore

private Vector<RecordListener> listeners;

private int lastModified = 0;
private long lastModified = 0;

private static int recordsOpened = 0;

Expand All @@ -74,6 +74,8 @@ private RecordStore(String recordStoreName, boolean createIfNecessary) throws Re

name = recordStoreName.replaceAll("[/\\\\:*?\"<>|]", "");

if(name == "") { return; }

appname = Mobile.getPlatform().loader.suitename;

rmsPath = Mobile.getPlatform().dataPath + "./rms/"+appname;
Expand Down Expand Up @@ -132,6 +134,9 @@ private RecordStore(String recordStoreName, boolean createIfNecessary) throws Re
loadRecord(data, offset, reclen);
offset+=reclen;
}

if(data.length - offset < 8) { lastModified = 0; } // For compatibility, as FreeJ2ME was saving records without the lastModified data for quite some time.
else { lastModified = getLong(data, offset); }
}
}
catch (Exception e)
Expand Down Expand Up @@ -167,6 +172,11 @@ private void save()
fout.write(records.get(i));
}

// last modified //
byte[] lastMod = new byte[8];
setLong(lastMod, 0, lastModified);
fout.write(lastMod);

fout.close();
}
catch (Exception e)
Expand All @@ -193,10 +203,39 @@ private int getUInt16(byte[] data, int offset)

return out | 0x00000000;
}

private void setUInt16(byte[] data, int offset, int val)
{
data[offset] = (byte)((val>>8) & 0xFF);
data[offset+1] = (byte)((val) & 0xFF);
data[offset+1] = (byte)((val) & 0xFF);
}

private long getLong(byte[] data, int offset)
{
long out = 0;

out |= (((long)data[offset]) & 0xFF) << 56;
out |= (((long)data[offset+1]) & 0xFF) << 48;
out |= (((long)data[offset+2]) & 0xFF) << 40;
out |= (((long)data[offset+3]) & 0xFF) << 32;
out |= (((long)data[offset+4]) & 0xFF) << 24;
out |= (((long)data[offset+5]) & 0xFF) << 16;
out |= (((long)data[offset+6]) & 0xFF) << 8;
out |= (((long)data[offset+7]) & 0xFF);

return out | 0x00000000;
}

private void setLong(byte[] data, int offset, long val)
{
data[offset] = (byte)((val>>56) & 0xFF);
data[offset+1] = (byte)((val>>48) & 0xFF);
data[offset+2] = (byte)((val>>40) & 0xFF);
data[offset+3] = (byte)((val>>32) & 0xFF);
data[offset+4] = (byte)((val>>24) & 0xFF);
data[offset+5] = (byte)((val>>16) & 0xFF);
data[offset+6] = (byte)((val>>8) & 0xFF);
data[offset+7] = (byte)((val) & 0xFF);
}

public int addRecord(byte[] data, int offset, int numBytes) throws RecordStoreException, RecordStoreFullException
Expand All @@ -220,12 +259,12 @@ public int addRecord(byte[] data, int offset, int numBytes) throws RecordStoreEx

records.addElement(rec);

lastModified = nextid;
lastModified = System.currentTimeMillis();
version++;

save();

for(int i=0; i<listeners.size(); i++) { listeners.get(i).recordAdded(this, lastModified); }
for(int i=0; i<listeners.size(); i++) { listeners.get(i).recordAdded(this, nextid); }

return nextid++;
}
Expand Down Expand Up @@ -316,6 +355,7 @@ public int getNumRecords()
public byte[] getRecord(int recordId) throws InvalidRecordIDException, RecordStoreException
{
if (!recordStoreIsOpen) { throw new RecordStoreNotOpenException("Cannot get the record of a closed Record Store"); }
if(recordId > records.size()-1) { throw new InvalidRecordIDException("setRecord: Invalid Record ID: "+recordId); }

Mobile.log(Mobile.LOG_DEBUG, RecordStore.class.getPackage().getName() + "." + RecordStore.class.getSimpleName() + ": " + "> getRecord("+recordId+")");

Expand All @@ -326,16 +366,16 @@ public byte[] getRecord(int recordId) throws InvalidRecordIDException, RecordSto
}
catch (Exception e)
{
Mobile.log(Mobile.LOG_DEBUG, RecordStore.class.getPackage().getName() + "." + RecordStore.class.getSimpleName() + ": " + "(getRecord) Record Store Exception: "+recordId);
e.printStackTrace();
Mobile.log(Mobile.LOG_ERROR, RecordStore.class.getPackage().getName() + "." + RecordStore.class.getSimpleName() + ": " + "(getRecord) Record Store Exception: "+recordId);
throw new RecordStoreException();
}
}

public int getRecord(int recordId, byte[] buffer, int offset) throws InvalidRecordIDException, RecordStoreException
{
if (!recordStoreIsOpen) { throw new RecordStoreNotOpenException("Cannot get the record of a closed Record Store"); }

if(recordId > records.size()-1) { throw new InvalidRecordIDException("setRecord: Invalid Record ID: "+recordId); }

Mobile.log(Mobile.LOG_DEBUG, RecordStore.class.getPackage().getName() + "." + RecordStore.class.getSimpleName() + ": " + "> getRecord(id, buffer, offset)");
byte[] temp = getRecord(recordId);

Expand All @@ -353,9 +393,11 @@ public int getRecord(int recordId, byte[] buffer, int offset) throws InvalidReco
public int getRecordSize(int recordId) throws InvalidRecordIDException, RecordStoreException
{
if (!recordStoreIsOpen) { throw new RecordStoreNotOpenException("Cannot get the record's size on a closed Record Store"); }
if(recordId > records.size()-1) { throw new InvalidRecordIDException("setRecord: Invalid Record ID: "+recordId); }

Mobile.log(Mobile.LOG_DEBUG, RecordStore.class.getPackage().getName() + "." + RecordStore.class.getSimpleName() + ": " + "> Get Record Size");
return getRecord(recordId).length;

return records.get(recordId).length;
}

public int getSize() throws RecordStoreNotOpenException
Expand Down Expand Up @@ -489,7 +531,7 @@ public void setRecord(int recordId, byte[] newData, int offset, int numBytes) th
Mobile.log(Mobile.LOG_ERROR, RecordStore.class.getPackage().getName() + "." + RecordStore.class.getSimpleName() + ": " + "Problem in Set Record");
e.printStackTrace();
}
lastModified = recordId;
lastModified = System.currentTimeMillis();
save();
for(int i=0; i<listeners.size(); i++)
{
Expand Down

0 comments on commit 661761d

Please sign in to comment.