public static string serializeToJson(ExportableTimingSheet obj, string fileName) { // create output filestream fileName = getFilePath(fileName); System.IO.FileStream file = System.IO.File.Create(rootPath + fileName); // instantiate JSON serializer var serializer = new DataContractJsonSerializer(typeof(ExportableTimingSheet)); serializer.WriteObject(file, obj); // close file stream & return file name file.Close(); return(fileName); }
/// <summary> /// Serialize and export timing data of current checkpoints to JSON file. /// </summary> public void exportTimingSheet() { // compute the hash code of this race int raceHash = GetHashCode(); // build instance of ExportableTimingSheet ExportableTimingSheet timingSheet = new ExportableTimingSheet() { exportDatetime = DateTime.UtcNow, raceHashCode = GetHashCode(), timingData = markedSectorCheckpoints.Select(chkpt => chkpt.getSimplifiedTimingData()).ToArray() }; // export file TimingSheetExporter.serializeToJson(timingSheet, raceHash.ToString()); }
/// <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 { } }