/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ using System; using System.ComponentModel; using OmiLAXR.Composers; using OmiLAXR.Utils; using UnityEngine; namespace OmiLAXR.Endpoints { /// /// Debug endpoint that prints all received statements to the Unity Editor console. /// Provides multiple formatting options for statement visualization during development and testing. /// Useful for debugging statement flow, verifying data content, and monitoring system behavior. /// [AddComponentMenu("OmiLAXR / 6) Endpoints / Statement Printer")] [Description("Prints all received statements to Unity Editor console. May be used for testing purposes.")] public class StatementPrinter : Endpoint { /// /// Conservative batch size for console output to prevent overwhelming the Unity console. /// Lower than typical endpoints to maintain readable console output during debugging. /// protected override int MaxBatchSize => 10; /// /// Available formatting options for statement output. /// Each type provides different levels of detail and formatting styles for various debugging needs. /// [Serializable] public enum PrintType { /// /// Standard ToString() representation of the statement. /// Provides basic statement information in the default format. /// Default, /// /// Condensed representation with key information only. /// Useful for quick overview without overwhelming detail. /// Short, /// /// Full JSON serialization of the statement. /// Provides complete data structure visibility for detailed analysis. /// Json, /// /// CSV format with standard structure preservation. /// Shows how the statement would appear in CSV export format. /// Maintains object hierarchy and nested structures. /// Csv, /// /// Flattened CSV format with all nested structures expanded. /// Provides comprehensive field-by-field view of all statement data. /// Useful for understanding complete data transformation. /// CsvFlat } /// /// Selected print format for statement output. /// Configurable in the Unity Inspector to switch between different visualization modes. /// public PrintType printType = PrintType.Default; /// /// Handles the processing of individual statements for console output. /// Applies the selected formatting and prints the result to the Unity console. /// Always returns success as console printing cannot fail in typical scenarios. /// /// The statement to process and display /// Always returns TransferCode.Success as console output cannot fail protected override TransferCode HandleSending(IStatement statement) { // Process the statement based on the selected print format switch (printType) { case PrintType.Json: // Convert statement to JSON string for detailed structure analysis PrintStatement(statement.ToJsonString()); break; case PrintType.Short: // Use condensed format for quick overview PrintStatement(statement.ToShortString()); break; case PrintType.Csv: { // Convert to CSV format maintaining original structure var csvFormat = statement.ToCsvFormat(); PrintStatement(csvFormat.ToString()); break; } case PrintType.CsvFlat: { // Convert to flattened CSV format with all nested data expanded // The 'true' parameter enables deep flattening of complex objects var csvFormat = statement.ToCsvFormat(true); PrintStatement(csvFormat.ToString()); break; } default: // Use default ToString() implementation for standard representation PrintStatement(statement.ToString()); break; } // Console printing operations don't fail, so always return success return TransferCode.Success; } /// /// Static utility method for consistent statement printing to the console. /// Prefixes all output with "Sent statement:" for easy identification in console logs. /// Uses the OmiLAXR debug logging system for consistent formatting and filtering. /// /// The formatted statement string to print private static void PrintStatement(string message) => DebugLog.OmiLAXR.Print("Sent statement: " + message); public override DataMap ProvideDataMap() { return new DataMap() { ["printType"] = printType.ToString() }; } public override void ConsumeDataMap(DataMap map) { if (Enum.TryParse(map["printType"] as string, out PrintType p)) printType = p; } } }