/// <summary> /// Export the current race to JSON. User will be prompted to enter a name. /// </summary> public void exportRace() { // validate checkpoints to make sure the race is valid if (!validateCheckpoints(markedSectorCheckpoints)) { GTA.UI.Notification.Show("~r~Lap Timer: cannot export race because validation failed."); return; } // prompt user to enter a name for the race string name = GTA.Game.GetUserInput("custom_race"); // export the race using RaceExporter string fileName = RaceExporter.serializeToJson(RaceExporter.createExportableRace(name, markedSectorCheckpoints, lapRace), name); // inform user of the exported file GTA.UI.Notification.Show("Lap Timer: exported race as " + fileName); }
/// <summary> /// Import a race from a file on disk. The currently placed checkpoints will be overwritten. /// </summary> public void importRace(string path = null) { // clean up any existing race/checkpoints if (raceMode) { exitRaceMode(); } clearAllSectorCheckpoints(); // set placement mode active; make sure player is not in race mode (exit if need to) placementMode = true; // prompt user to enter the name of the file (with or without the file extension) to import from string name = path == null?GTA.Game.GetUserInput("custom_race") : path; try { // attempt to import from file ExportableRace race = RaceExporter.deserializeFromJson(name, path == null ? false : true); // repopulate List<SectorCheckpoint> using the imported race data lapRace = race.lapMode; for (int i = 0; i < race.checkpoints.Length; i++) { SimplifiedCheckpoint sc = race.checkpoints[i]; SectorCheckpoint chkpt = new SectorCheckpoint(sc.number, sc.position, sc.quarternion, false); markedSectorCheckpoints.Add(chkpt); } // inform user of successful load GTA.UI.Notification.Show("Lap Timer: successfully imported race!"); // with the race loaded & reconstructed, try to load timing sheet. make sure all hash codes match! int raceHash = GetHashCode(); ExportableTimingSheet timingSheet = TimingSheetExporter.deserializeFromJson(raceHash.ToString()); for (int i = 0; i < timingSheet.timingData.Length; i++) { markedSectorCheckpoints[i].setTimingDataFromSimplified(timingSheet.timingData[i]); } GTA.UI.Notification.Show("Lap Timer: successfully imported personal timing sheet for the imported race!"); } catch { } }
private UIMenu buildRaceImportMenu(UIMenu submenu) { // get a List all races that can be imported List <ImportableRace> races = RaceExporter.getImportableRaces(); // iterate over each race & add to menu, along with their handlers foreach (ImportableRace r in races) { string descriptionString = r.name + "\nMode: " + (r.lapMode ? "circuit" : "point-to-point") + "\nVersion: " + r.version ?? "v1.x"; UIMenuItem item = new UIMenuItem(r.name, descriptionString); item.Activated += (menu, sender) => { race.importRace(r.filePath); _menuPool.CloseAllMenus(); }; submenu.AddItem(item); } return(submenu); }