/** * Serialize RecordedEvents. * @param recordingPath path where the data should be saved. */ public void serializeRecordedEvents(String recordingPath) { String filepath = recordingPath + EVENT_DIR + recordAndReplayUtils.getFileCount() + JSON; recordAndReplayUtils.setFileAmount(recordAndReplayUtils.getFileAmount() + 1); recordAndReplayUtils.setFileCount(recordAndReplayUtils.getFileCount() + 1); recordedEventSerializer.serializeRecordedEvents(recordedEventStore.popEvents(), filepath); logger.info("RecordedEvents Serialization completed!"); }
private void saveRecordingData() { if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.RECORDING) { if (recordAndReplayUtils.isShutdownRequested()) { recordAndReplaySerializer.serializeRecordAndReplayData(); recordAndReplayCurrentStatus.setStatus(RecordAndReplayStatus.NOT_ACTIVATED); recordAndReplayUtils.reset(); } else { String recordingPath = PathManager.getInstance().getRecordingPath(recordAndReplayUtils.getGameTitle()).toString(); recordAndReplaySerializer.serializeRecordedEvents(recordingPath); } } }
/** * Deserialize RecordedEvents. * @param recordingPath path where the data was saved. */ void deserializeRecordedEvents(String recordingPath) { String filepath = recordingPath + EVENT_DIR + recordAndReplayUtils.getFileCount() + JSON; recordAndReplayUtils.setFileCount(recordAndReplayUtils.getFileCount() + 1); recordedEventStore.setEvents(recordedEventSerializer.deserializeRecordedEvents(filepath)); logger.info("RecordedEvents Deserialization completed!"); }
private boolean isReplay() { boolean isReplay = false; if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAY_FINISHED || recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING) { isReplay = true; if (recordAndReplayUtils.isShutdownRequested()) { recordAndReplayCurrentStatus.setStatus(RecordAndReplayStatus.NOT_ACTIVATED); recordAndReplayUtils.reset(); } } return isReplay; }
RecordAndReplayUtils recordAndReplayUtils = new RecordAndReplayUtils(); rootContext.put(RecordAndReplayUtils.class, recordAndReplayUtils); CharacterStateEventPositionMap characterStateEventPositionMap = new CharacterStateEventPositionMap();
/** * Processes recorded events for a certain amount of time and only if the timestamp is right. */ private void processRecordedEvents() { if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING && !this.areRecordedEventsLoaded) { initialiseReplayData(); } //If replay is ready, process some recorded events if the time is right. if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING) { processRecordedEventsBatch(1); if (this.recordedEvents.isEmpty()) { if (recordAndReplayUtils.getFileCount() <= recordAndReplayUtils.getFileAmount()) { //Get next recorded events file loadNextRecordedEventFile(); } else { finishReplay(); } } } }
private void loadNextRecordedEventFile() { String recordingPath = PathManager.getInstance().getRecordingPath(recordAndReplayUtils.getGameTitle()).toString(); recordAndReplaySerializer.deserializeRecordedEvents(recordingPath); fillRecordedEvents(); }
private void loadGame(GameInfo item) { try { GameManifest manifest = item.getManifest(); recordAndReplayUtils.setGameTitle(manifest.getTitle()); config.getWorldGeneration().setDefaultSeed(manifest.getSeed()); config.getWorldGeneration().setWorldTitle(manifest.getTitle()); CoreRegistry.get(GameEngine.class).changeState(new StateLoading(manifest, NetworkMode.NONE)); } catch (Exception e) { logger.error("Failed to load saved game", e); getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Error Loading Game", e.getMessage()); } }
private void deserializeFileAmount(Gson gson, String recordingPath) { try (FileReader fileReader = new FileReader(recordingPath + FILE_AMOUNT)){ JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(fileReader); Type typeOfCount = new TypeToken<Integer>() { }.getType(); recordAndReplayUtils.setFileAmount(gson.fromJson(jsonElement, typeOfCount)); logger.info("File Amount Deserialization completed!"); } catch (Exception e) { logger.error("Error while deserializing file amount:", e); } }
private void serializeFileAmount(Gson gson, String recordingPath) { try { JsonWriter writer = new JsonWriter(new FileWriter(recordingPath + FILE_AMOUNT)); gson.toJson(recordAndReplayUtils.getFileAmount(), Integer.class, writer); writer.close(); logger.info("File Amount Serialization completed!"); } catch (Exception e) { logger.error("Error while serializing file amount:", e); } }
@Override protected void setupStorageManager() throws IOException { ModuleManager moduleManager = context.get(ModuleManager.class); EngineEntityManager engineEntityManager = context.get(EngineEntityManager.class); BlockManager blockManager = context.get(BlockManager.class); BiomeManager biomeManager = context.get(BiomeManager.class); RecordAndReplaySerializer recordAndReplaySerializer = context.get(RecordAndReplaySerializer.class); Path savePath = PathManager.getInstance().getSavePath("world1"); RecordAndReplayUtils recordAndReplayUtils = new RecordAndReplayUtils(); RecordAndReplayCurrentStatus recordAndReplayCurrentStatus = context.get(RecordAndReplayCurrentStatus.class); ModuleEnvironment environment = context.get(ModuleManager.class).getEnvironment(); context.put(BlockFamilyLibrary.class, new BlockFamilyLibrary(environment,context)); ExtraBlockDataManager extraDataManager = context.get(ExtraBlockDataManager.class); context.put(StorageManager.class, new ReadWriteStorageManager(savePath, moduleManager.getEnvironment(), engineEntityManager, blockManager, biomeManager, extraDataManager, recordAndReplaySerializer, recordAndReplayUtils, recordAndReplayCurrentStatus)); }
/** * Serialize the recorded data. */ public void serializeRecordAndReplayData() { String recordingPath = PathManager.getInstance().getRecordingPath(recordAndReplayUtils.getGameTitle()).toString(); serializeRecordedEvents(recordingPath); Gson gson = new GsonBuilder().create(); serializeFileAmount(gson, recordingPath); serializeCharacterStateEventPositonMap(gson, recordingPath); serializeAttackEventExtraRecorder(gson, recordingPath); }
/** * Last step of the recording setup process. Copies the save files from the selected game, transplants them into the 'recordings' folder, and renames the map files * to match the provided recording name. Then launches the game loading state. * * @param newTitle The title of the new recording. */ private void loadGame(String newTitle) { try { final GameManifest manifest = gameInfo.getManifest(); copySaveDirectoryToRecordingLibrary(manifest.getTitle(), newTitle); recordAndReplayUtils.setGameTitle(newTitle); config.getWorldGeneration().setDefaultSeed(manifest.getSeed()); config.getWorldGeneration().setWorldTitle(newTitle); CoreRegistry.get(GameEngine.class).changeState(new StateLoading(manifest, NetworkMode.NONE)); } catch (Exception e) { logger.error("Failed to load saved game", e); getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Error Loading Game", e.getMessage()); } }
recordAndReplayUtils = new RecordAndReplayUtils(); CharacterStateEventPositionMap characterStateEventPositionMap = new CharacterStateEventPositionMap(); DirectionAndOriginPosRecorderList directionAndOriginPosRecorderList = new DirectionAndOriginPosRecorderList();
/** * Deserialize recorded data. */ public void deserializeRecordAndReplayData() { String recordingPath = PathManager.getInstance().getRecordingPath(recordAndReplayUtils.getGameTitle()).toString(); deserializeRecordedEvents(recordingPath); Gson gson = new GsonBuilder().create(); deserializeFileAmount(gson, recordingPath); deserializeCharacterStateEventPositonMap(gson, recordingPath); deserializeAttackEventExtraRecorder(gson, recordingPath); }
recordAndReplayCurrentStatus = new RecordAndReplayCurrentStatus(); RecordedEventStore eventStore = new RecordedEventStore(); RecordAndReplayUtils recordAndReplayUtils = new RecordAndReplayUtils(); CharacterStateEventPositionMap characterStateEventPositionMap = new CharacterStateEventPositionMap(); DirectionAndOriginPosRecorderList directionAndOriginPosRecorderList = new DirectionAndOriginPosRecorderList();
ExtraBlockDataManager extraDataManager = context.get(ExtraBlockDataManager.class); RecordedEventStore recordedEventStore = new RecordedEventStore(); RecordAndReplayUtils recordAndReplayUtils = new RecordAndReplayUtils(); context.put(RecordAndReplayUtils.class, recordAndReplayUtils); CharacterStateEventPositionMap characterStateEventPositionMap = new CharacterStateEventPositionMap();