public static void SaveLevel(string filepath, Level level) { JsonObject rootObject = new JsonObject(); JsonArray textureArray = new JsonArray(); JsonArray voxelArray = new JsonArray(); List <(VoxelPos, Voxel)> voxelList = level.GetVoxelList(); List <string> textures = new List <string>(); foreach ((VoxelPos, Voxel)pair in voxelList) { VoxelPos pos = pair.Item1; Voxel voxel = pair.Item2; JsonObject voxelObject = new JsonObject(); JsonObject posObject = new JsonObject(); JsonArray faceArray = new JsonArray(); posObject["x"] = pos.x; posObject["y"] = pos.y; posObject["z"] = pos.z; voxelObject["pos"] = posObject; foreach (string face in new string[] { voxel.frontTexture, voxel.backTexture, voxel.leftTexture, voxel.rightTexture, voxel.topTexture, voxel.bottomTexture }) { if (!textures.Contains(face)) { textures.Add(face); } faceArray.Add(new JsonPrimitive(textures.IndexOf(face))); } voxelObject["faces"] = faceArray; voxelArray.Add(voxelObject); } textures.ForEach((texture) => textureArray.Add(new JsonPrimitive(texture))); rootObject.Add("textures", textureArray); rootObject.Add("voxels", voxelArray); Godot.File file = new Godot.File(); file.Open("user://save.json", Godot.File.ModeFlags.Write); file.StoreString(rootObject.ToString()); file.Close(); }
// Creates our time-stamped log file and opens it for writing private static void InitLogFile(string LogDir) { RandomNumberGenerator rnd = new RandomNumberGenerator(); rnd.Randomize(); FullLogFilePath = $"{LogDir}{DateTime.Now.ToString("yyyy.MM.dd-HH.mm.ss")}_{rnd.RandiRange(10000,99999).ToString()}{EXT_LOG}"; if (CreateLogDirectoryIfNotExists(LogDir)) { LogFile = new File(); LogFile.Open(FullLogFilePath, File.ModeFlags.Write); Log(LOG_CAT, MDLogLevel.Info, $"Created log file {FullLogFilePath}"); LogFile.Close(); } else { Console.WriteLine("Failed to create log directory."); } }
private static File GetHistoryFile() { if (CreateHistoryDirectoryIfNotExists(HISTORY_DIR)) { string FullFilePath = HISTORY_DIR + HISTORY_FILE; File CmdFile = new File(); if (!CmdFile.FileExists(FullFilePath)) { CmdFile.Open(FullFilePath, File.ModeFlags.Write); CmdFile.Close(); } CmdFile.Open(FullFilePath, File.ModeFlags.ReadWrite); return(CmdFile); } MDLog.Error(LOG_CAT, "Failed to create command history directory."); return(null); }
/// <summary> /// Get history of commands executed /// </summary> /// <returns>List of commands executed</returns> public static List <string> GetCommandHistory() { File CmdFile = GetHistoryFile(); if (CmdFile == null) { return(null); } string HistoryText = CmdFile.GetAsText(); CmdFile.Close(); List <string> CommandHistory = new List <string>(HistoryText.Split('\n')); CommandHistory = CommandHistory.Where(Command => !string.IsNullOrWhiteSpace(Command)).ToList(); CommandHistory.Reverse(); return(CommandHistory); }
private static void AddCommandToHistory(string Command) { List <string> History = GetCommandHistory(); if (History.Count > 0 && History[0].Equals(Command)) { // Don't store the last as last command return; } File CmdFile = GetHistoryFile(); if (CmdFile == null) { return; } CmdFile.SeekEnd(); CmdFile.StoreLine(Command); CmdFile.Close(); }
// Writes the message to the log file private static void LogToFile(string Message) { OpenLogFile(); LogFile?.StoreLine(Message); LogFile?.Close(); }