RoomService v2.3.1
Loading...
Searching...
No Matches
RoomService.SaveLogger Class Reference

Lua function to save the time logger to disk.

Member Function Documentation

◆ Implementation()

void RoomService.SaveLogger.Implementation ( string fileName)
inlineprivate

Usage:

Lua function to save the time logger to disk.
Definition RoomServiceLuaFunctions.cs:887
Definition MyPluginInfo.cs:2

Source Code

902 {
903 if (!RoomServiceUtils.IsOnlineHost())
904 {
905 return;
906 }
907
908 // Remove invalid characters from the file name
909 string sanitizedFileName = string.Concat(fileName.Split(Path.GetInvalidFileNameChars()));
910
911 // Ensure the file name ends with a .txt extension
912 if (!sanitizedFileName.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
913 {
914 sanitizedFileName += ".json";
915 }
916
917 // Get the plugin folder path and combine it with the sanitized file name
918 string pluginFolderPath = BepInEx.Paths.PluginPath;
919 string filePath = Path.Combine(pluginFolderPath, sanitizedFileName);
920
921 // Combine the dictionaries into a JSON-serializable structure
922 var combinedData = new List<object>();
923
924 foreach (var level in Plugin.Instance.levelInfo)
925 {
926 var hash = level.Key;
927 var levelData = level.Value;
928 var times = Plugin.Instance.playerTimes.ContainsKey(hash) ? Plugin.Instance.playerTimes[hash] : new List<PlayerTime>();
929
930 // Add the times directly into the level object
931 combinedData.Add(new
932 {
933 Hash = levelData.Hash,
934 Author = levelData.Author,
935 Name = levelData.Name,
936 Uid = levelData.Uid,
937 WorkshopId = levelData.WorkshopId,
938 Times = times
939 });
940 }
941
942 // Serialize to JSON
943 string json = JsonConvert.SerializeObject(combinedData, Formatting.Indented);
944 File.WriteAllText(filePath, json);
945 }