コード例 #1
0
        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();
        }
コード例 #2
0
        // 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.");
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
0
        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();
        }
コード例 #6
0
        /// <summary>
        /// Logs the message (supports formatting) in accordance with the LogProperties set for the specified log category
        /// </summary>
        /// <param name="CategoryName">The category to log in</param>
        /// <param name="LogLevel">The log level</param>
        /// <param name="Message">The message</param>
        /// <param name="Args">Arguments</param>
        public static void Log(string CategoryName, MDLogLevel LogLevel, string Message, params object[] Args)
        {
            // TODO - Get calling method's name automatically: https://stackoverflow.com/a/5443690
            bool            LogFile    = true;
            bool            LogConsole = true;
            MDConfiguration Config     = MDStatics.GetGameInstance().GetConfiguration();

            if (Config.HasValue(MDConfiguration.ConfigurationSections.Logging, CategoryName))
            {
                MDLogLevel ConfigLogLevel = Config.GetEnum <MDLogLevel>(MDConfiguration.ConfigurationSections.Logging, CategoryName, MDLogLevel.Trace);
                LogFile    = ConfigLogLevel <= LogLevel || LogLevel == MDLogLevel.Force;
                LogConsole = ConfigLogLevel <= LogLevel || LogLevel == MDLogLevel.Force;
            }
            else if (LogProperties.ContainsKey(CategoryName))
            {
                MDLogProperties LogProps = LogProperties[CategoryName];
                LogFile    = LogProps.FileLogLevel <= LogLevel || LogLevel == MDLogLevel.Force;
                LogConsole = LogProps.ConsoleLogLevel <= LogLevel || LogLevel == MDLogLevel.Force;
            }
            else
            {
                MDLogLevel DefaultLogLevel = Config.GetEnum <MDLogLevel>(MDConfiguration.ConfigurationSections.Logging, MDConfiguration.DEFAULT_LOG_LEVEL, MDLogLevel.Trace);
                LogFile    = DefaultLogLevel <= LogLevel || LogLevel == MDLogLevel.Force;
                LogConsole = DefaultLogLevel <= LogLevel || LogLevel == MDLogLevel.Force;
            }

            if (LogFile || LogConsole)
            {
                int       PeerID   = MDStatics.GetPeerId();
                MDNetMode NetMode  = MDStatics.GetNetMode();
                string    ClientID = "PEER " + PeerID;
                if (NetMode == MDNetMode.Standalone)
                {
                    ClientID = "STANDALONE";
                }
                else if (NetMode == MDNetMode.Server)
                {
                    ClientID = "SERVER";
                }

                string FullMessage = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "]" +
                                     "[" + (Godot.Engine.GetIdleFrames() % 1000).ToString("D3") + "]" +
                                     "[" + ClientID + "] " +
                                     "[" + CategoryName + "::" + LogLevel + "] " +
                                     string.Format(Message, Args);

                if (LogFile)
                {
                    LogToFile(FullMessage);
                }

                if (LogConsole)
                {
                    LogToConsole(FullMessage);
                }
            }

            if (LogLevel == MDLogLevel.Fatal)
            {
                DebugBreak();
            }
        }
コード例 #7
0
 public GodotFileStream(File file)
 {
     this.file = file;
 }