Refactor: Use enum instead of int for result of loading a savegame

This commit is contained in:
Oskar Wiksten
2013-11-03 14:59:14 +01:00
parent 6006fe1eb5
commit eba387159e
5 changed files with 21 additions and 19 deletions

View File

@@ -26,7 +26,7 @@ public final class WorldSetup {
public int loadFromSlot = Savegames.SLOT_QUICKSAVE;
public boolean isSceneReady = false;
public String newHeroName;
private int loadResult;
private Savegames.LoadSavegameResult loadResult;
public WorldSetup(WorldContext world, ControllerContext controllers, Context androidContext) {
this.world = world;
@@ -105,7 +105,7 @@ public final class WorldSetup {
if (world.model != null) world.resetForNewGame();
if (createNewCharacter) {
createNewWorld();
loadResult = Savegames.LOAD_RESULT_SUCCESS;
loadResult = Savegames.LoadSavegameResult.success;
} else {
loadResult = continueWorld();
}
@@ -126,7 +126,7 @@ public final class WorldSetup {
onSceneLoadedListener = null;
if (o == null) return;
if (loadResult == Savegames.LOAD_RESULT_SUCCESS) {
if (loadResult == Savegames.LoadSavegameResult.success) {
o.onSceneLoaded();
} else {
o.onSceneLoadFailed(loadResult);
@@ -136,7 +136,7 @@ public final class WorldSetup {
}).execute();
}
private int continueWorld() {
private Savegames.LoadSavegameResult continueWorld() {
Context ctx = androidContext.get();
return Savegames.loadWorld(world, controllers, ctx, loadFromSlot);
}
@@ -154,7 +154,7 @@ public final class WorldSetup {
public interface OnSceneLoadedListener {
void onSceneLoaded();
void onSceneLoadFailed(int loadResult);
void onSceneLoadFailed(Savegames.LoadSavegameResult loadResult);
}
public interface OnResourcesLoadedListener {
void onResourcesLoaded();

View File

@@ -58,7 +58,7 @@ public final class LoadSaveActivity extends Activity implements OnClickListener
slotList.removeView(slotTemplateButton);
slotList.removeView(createNewSlot);
addSavegameSlotButtons(slotList, params, Savegames.getUsedSavegameSlots(this));
addSavegameSlotButtons(slotList, params, Savegames.getUsedSavegameSlots());
if (!isLoading) {
Button b = new Button(this);
@@ -86,7 +86,7 @@ public final class LoadSaveActivity extends Activity implements OnClickListener
public void loadsave(int slot) {
if (slot == SLOT_NUMBER_CREATE_NEW_SLOT) {
List<Integer> usedSlots = Savegames.getUsedSavegameSlots(this);
List<Integer> usedSlots = Savegames.getUsedSavegameSlots();
if (usedSlots.isEmpty()) slot = SLOT_NUMBER_FIRST_SLOT;
else slot = Collections.max(usedSlots) + 1;
}

View File

@@ -55,9 +55,9 @@ public final class LoadingActivity extends Activity implements OnResourcesLoaded
}
@Override
public void onSceneLoadFailed(int loadResult) {
public void onSceneLoadFailed(Savegames.LoadSavegameResult loadResult) {
progressDialog.dismiss();
if (loadResult == Savegames.LOAD_RESULT_FUTURE_VERSION) {
if (loadResult == Savegames.LoadSavegameResult.savegameIsFromAFutureVersion) {
showLoadingFailedDialog(R.string.dialog_loading_failed_incorrectversion);
} else {
showLoadingFailedDialog(R.string.dialog_loading_failed_message);

View File

@@ -161,7 +161,7 @@ public final class StartScreenActivity extends Activity {
Dialogs.showNewVersion(this);
}
boolean hasSavegames = !Savegames.getUsedSavegameSlots(this).isEmpty();
boolean hasSavegames = !Savegames.getUsedSavegameSlots().isEmpty();
startscreen_load.setEnabled(hasSavegames);
}

View File

@@ -20,9 +20,11 @@ import java.util.regex.Pattern;
public final class Savegames {
public static final int SLOT_QUICKSAVE = 0;
public static final int LOAD_RESULT_SUCCESS = 0;
public static final int LOAD_RESULT_UNKNOWN_ERROR = 1;
public static final int LOAD_RESULT_FUTURE_VERSION = 2;
public static enum LoadSavegameResult {
success
, unknownError
, savegameIsFromAFutureVersion
}
public static boolean saveWorld(WorldContext world, Context androidContext, int slot, String displayInfo) {
try {
@@ -42,10 +44,10 @@ public final class Savegames {
return false;
}
}
public static int loadWorld(WorldContext world, ControllerContext controllers, Context androidContext, int slot) {
public static LoadSavegameResult loadWorld(WorldContext world, ControllerContext controllers, Context androidContext, int slot) {
try {
FileInputStream fos = getInputFile(androidContext, slot);
int result = loadWorld(androidContext.getResources(), world, controllers, fos);
LoadSavegameResult result = loadWorld(androidContext.getResources(), world, controllers, fos);
fos.close();
return result;
} catch (IOException e) {
@@ -56,7 +58,7 @@ public final class Savegames {
e.printStackTrace(pw);
L.log("Load error: " + sw.toString());
}
return LOAD_RESULT_UNKNOWN_ERROR;
return LoadSavegameResult.unknownError;
}
}
@@ -99,10 +101,10 @@ public final class Savegames {
dest.close();
}
public static int loadWorld(Resources res, WorldContext world, ControllerContext controllers, InputStream inState) throws IOException {
public static LoadSavegameResult loadWorld(Resources res, WorldContext world, ControllerContext controllers, InputStream inState) throws IOException {
DataInputStream src = new DataInputStream(inState);
final FileHeader header = new FileHeader(src);
if (header.fileversion > AndorsTrailApplication.CURRENT_VERSION) return LOAD_RESULT_FUTURE_VERSION;
if (header.fileversion > AndorsTrailApplication.CURRENT_VERSION) return LoadSavegameResult.savegameIsFromAFutureVersion;
world.maps.readFromParcel(src, world, controllers, header.fileversion);
world.model = new ModelContainer(src, world, controllers, header.fileversion);
@@ -110,7 +112,7 @@ public final class Savegames {
onWorldLoaded(res, world, controllers);
return LOAD_RESULT_SUCCESS;
return LoadSavegameResult.success;
}
private static void onWorldLoaded(Resources res, WorldContext world, ControllerContext controllers) {