Skip to content

MrCroft/CSharp2CPP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

C# to C++ Transpiler for SEGA Dreamcast

Advanced C# to C++ Transpiler v3.0
Tool made by Mr.Croft

A powerful Unity Editor tool that converts C# MonoBehaviour scripts into production-ready (or at least 80% of it) C++ code for SEGA Dreamcast development. This transpiler bridges the gap between modern Unity development and retro Dreamcast homebrew programming.

๐ŸŽฏ Overview

This transpiler is specifically designed for developers porting Unity games to the SEGA Dreamcast. It provides two conversion modes:

  • Advanced Parser Mode (v2.1) - Fast, Dreamcast-optimized, lightweight regex-based conversion
  • IL2CPP Backend Mode (v3.0) - Unity's official IL2CPP compiler for production-grade, semantically accurate C++ output

โœจ Key Features

๐Ÿ”ง Core Capabilities

  • โœ… Complete Class Conversion - Converts MonoBehaviour, Component, ScriptableObject, and regular classes
  • โœ… Method Translation - Translates methods with full parameter support and return types
  • โœ… Field & Property Mapping - Converts fields, properties (getters/setters), and automatic properties
  • โœ… Unity Lifecycle Support - Handles Awake, Start, Update, FixedUpdate, OnDestroy, OnCollisionEnter, etc.
  • โœ… Type Mapping - Automatic conversion of Unity types to Dreamcast equivalents
    • Vector3 โ†’ vec3_t
    • GameObject โ†’ game_object_t*
    • Transform โ†’ transform_t*
    • Rigidbody โ†’ rigidbody_t*
    • And 60+ more type mappings
  • โœ… Unity API Translation - Converts common Unity APIs to Dreamcast equivalents
    • Time.deltaTime โ†’ deltaTime
    • Debug.Log() โ†’ debug_log()
    • Vector3.zero โ†’ vec3_zero()
    • Physics.Raycast() โ†’ physics_raycast()
    • And 50+ more API mappings

๐Ÿ“ฆ Advanced Features

  • ๐Ÿ” Nested Classes & Structs - Preserves nested type definitions
  • ๐ŸŽฒ Enum Support - Converts C# enums to C++ enum classes
  • ๐Ÿ”„ Coroutine Detection - Identifies coroutine patterns (requires manual implementation)
  • ๐ŸŽฎ Input System Support - Handles Unity's new Input System (InputAction, PlayerControls, etc.)
  • ๐Ÿ“Š Component References - Converts GetComponent() patterns
  • โšก Performance Optimizations - Dreamcast-specific code generation
  • ๐Ÿ“ Code Documentation - Generates warnings and TODO lists for manual review

๐Ÿ–ฅ๏ธ User Interface

  • ๐ŸŽจ Clean Editor Window - Integrated Unity Editor GUI
  • ๐Ÿ“‚ Drag & Drop - Simple MonoScript selection
  • ๐Ÿ’พ Export Options - Save header (.h) and implementation (.cpp) files
  • ๐Ÿ“Š Multi-Tab View - Separate tabs for Header, Implementation, Warnings, and TODOs
  • ๐Ÿ”„ Real-time Conversion - Instant feedback on code conversion
  • ๐Ÿ“‹ Copy to Clipboard - Quick copy buttons for all outputs

๐Ÿ“ Components

Core Scripts

  1. AdvancedCppConverter.cs (~700 lines)

    • Main conversion engine
    • Type mapping dictionary (typeMap)
    • Unity API mapping (unityApiMap)
    • Header and implementation generation
  2. CppConverterParsing.cs (~400 lines)

    • Parsing utilities
    • Field/method/property extraction
    • Line-by-line code conversion
    • Syntax transformation helpers
  3. IL2CPPIntegration.cs (~300 lines)

    • Unity IL2CPP backend integration
    • Process management for il2cpp.exe
    • Generated code extraction
    • Error handling and diagnostics

Editor UI Scripts

  1. CSharpToCppTranspiler.cs (~200 lines)

    • Original Advanced Parser GUI (v2.1)
    • Menu: Dreamcast > Advanced C# to C++ Transpiler
  2. HybridCppTranspiler.cs (~350 lines)

    • Hybrid mode GUI with IL2CPP support (v3.0)
    • Menu: Dreamcast > Hybrid C# to C++ Transpiler v3.0

๐Ÿš€ Installation

Prerequisites

  • Unity 6000.1 or newer (tested on Unity 6000.1)
  • Windows, macOS, or Linux
  • IL2CPP build support (optional, for IL2CPP mode)

Setup

  1. Copy the folder
    Place the CSharp2CPP folder inside your Unity project:

    /Assets/DreamcastBridge/Dreamcast/Editor/CSharp2CPP/
    
  2. Wait for compilation
    Unity will automatically compile the scripts.

  3. Verify installation
    Check for new menu items:

    • Dreamcast > Advanced C# to C++ Transpiler
    • Dreamcast > Hybrid C# to C++ Transpiler v3.0

๐Ÿ“– Usage Guide

Basic Workflow

  1. Open the transpiler

    • Go to Window > Dreamcast > Hybrid C# to C++ Transpiler v3.0
  2. Select your C# script

    • Drag & drop a MonoScript into the "C# Script" field
    • Or click the field to browse
  3. Choose conversion mode

    • Advanced Parser - For Dreamcast-optimized, lightweight output
    • IL2CPP Backend - For Unity-accurate, production code
  4. Convert

    • Click "Convert with Advanced Parser" or "Convert with IL2CPP"
    • Wait for conversion to complete
  5. Review output

    • Check the Header tab for the .h file
    • Check the Implementation tab for the .cpp file
    • Review Warnings for potential issues
    • Check TODOs for manual implementation tasks
  6. Export

    • Click "Save Header (.h)" and "Save Implementation (.cpp)"
    • Choose destination folder
    • Files are ready for Dreamcast compilation!

๐Ÿ”€ Conversion Modes

Mode 1: Advanced Parser (Recommended for Dreamcast)

Best for: Final Dreamcast builds, memory-constrained environments

Characteristics:

  • โœ… Fast conversion (~1-2 seconds)
  • โœ… Dreamcast-optimized code
  • โœ… Minimal memory footprint
  • โœ… No external dependencies
  • โœ… Direct C++ output
  • โš ๏ธ May require manual fixes for complex logic

When to use:

  • You need compact, Dreamcast-ready code
  • Memory is critical (Dreamcast has 16MB RAM)
  • You want full control over the C++ output
  • You're willing to manually implement complex features

Example output:

// Header (.h)
struct player_controller_t : public component_t
{
    float moveSpeed;
    vec3_t velocity;
    
    void move(vec3_t direction);
    void jump();
};

// Implementation (.cpp)
void player_controller_t::move(vec3_t direction)
{
    velocity = vec3_scale(direction, moveSpeed);
}

Mode 2: IL2CPP Backend (Reference/Testing)

Best for: Reference code, understanding Unity semantics, testing

Characteristics:

  • โœ… Unity-accurate conversion
  • โœ… Production-grade C++ output
  • โœ… Handles complex C# features
  • โœ… Complete Unity API mapping
  • โŒ Requires libil2cpp runtime (~30MB)
  • โŒ Not practical for Dreamcast (16MB RAM limit)

When to use:

  • You need to understand how Unity handles complex code
  • You want reference implementations for manual porting
  • You're testing conversion accuracy
  • You're developing for other platforms first

โš ๏ธ Important: IL2CPP mode is NOT suitable for final Dreamcast builds due to memory requirements. Use it as a reference, then implement optimized versions with Advanced Parser.

Example output:

// IL2CPP generates complete Unity runtime code
// with all safety checks, metadata, and runtime features
// Great for reference, but too large for Dreamcast

๐ŸŽฎ Hybrid Workflow (Recommended)

For best results, use both modes together:

  1. Start with IL2CPP (Reference)

    • Convert your script with IL2CPP mode
    • Study the generated code
    • Understand Unity's implementation of complex features
  2. Implement with Advanced Parser (Production)

    • Convert the same script with Advanced Parser
    • Use IL2CPP output as reference for manual fixes
    • Optimize for Dreamcast constraints
    • Test on hardware
  3. Iterate

    • Refine the C++ code
    • Add Dreamcast-specific optimizations
    • Remove unnecessary Unity abstractions

โš ๏ธ Limitations & Manual Work Required

What the Transpiler Handles

โœ… Class structure and hierarchy
โœ… Field and property declarations
โœ… Method signatures and basic logic
โœ… Type conversions
โœ… Unity lifecycle method detection
โœ… Basic API mappings

What Requires Manual Implementation

โŒ Unity Runtime - You must implement the Dreamcast equivalents:

  • game_object_t, transform_t, component_t structures
  • Physics system (rigidbody_t, collision detection)
  • Input handling (input_action_t, controller support)
  • Rendering system (mesh, materials, textures)

โŒ Complex Logic - Advanced C# features may need manual porting:

  • Coroutines (requires state machine implementation)
  • LINQ queries (convert to loops)
  • Lambda expressions (convert to function pointers)
  • Events and delegates (implement callback systems)
  • Reflection (not supported on Dreamcast)

โŒ Platform-Specific Code - Dreamcast has unique constraints:

  • Memory management (16MB RAM total)
  • VMU (Visual Memory Unit) integration
  • Controller vibration (jump pack)
  • Network play (modem/broadband adapter)

๐Ÿ› ๏ธ Type Mappings

Primitive Types

C# Type C++ Type
int int32_t
float float
bool bool
string std::string
void void

Unity Math Types

C# Type Dreamcast C++ Type
Vector2 vec2_t
Vector3 vec3_t
Vector4 vec4_t
Quaternion quat_t
Color color_t
Matrix4x4 matrix4x4_t

Unity Components

C# Type Dreamcast C++ Type
GameObject game_object_t*
Transform transform_t*
Rigidbody rigidbody_t*
Collider collider_t*
CharacterController character_controller_t*
AudioSource audio_source_t*
Camera camera_t*

Unity APIs

C# API Dreamcast C++ API
Time.deltaTime deltaTime
Debug.Log() debug_log()
Vector3.zero vec3_zero()
Physics.Raycast() physics_raycast()
Input.GetKey() is_key_down()
Instantiate() instantiate_game_object()
Destroy() destroy_game_object()

๐Ÿ› Troubleshooting

Issue: IL2CPP Mode Not Available

Solution:

  • Install IL2CPP build support in Unity Hub
  • Verify installation path in transpiler warnings
  • Use Advanced Parser mode as fallback

Issue: Conversion Errors

Solution:

  • Check C# script for syntax errors
  • Ensure script compiles in Unity first
  • Review warnings tab for specific issues
  • Try simpler test scripts first

Issue: Generated Code Won't Compile

Solution:

  • Implement missing Dreamcast types (game_object_t, vec3_t, etc.)
  • Review TODOs tab for manual implementation tasks
  • Check type mappings are correct
  • Verify header includes are available

๐Ÿ“š Documentation

For detailed guides and workflows, see the Pages documentation:


๐ŸŽฏ Best Practices

Do's โœ…

  • โœ… Start with simple scripts to test the workflow
  • โœ… Review all warnings and TODOs before using generated code
  • โœ… Use IL2CPP mode for reference, Advanced Parser for production
  • โœ… Implement Dreamcast runtime incrementally
  • โœ… Test frequently on Dreamcast hardware
  • โœ… Keep C# code Dreamcast-friendly (avoid heavy allocations)
  • โœ… Document your manual fixes for future reference

Don'ts โŒ

  • โŒ Don't blindly use generated code without review
  • โŒ Don't use IL2CPP runtime on Dreamcast (memory limit)
  • โŒ Don't expect 100% automatic conversion
  • โŒ Don't ignore warnings and TODOs
  • โŒ Don't use reflection or heavy C# features
  • โŒ Don't allocate large amounts of memory per frame
  • โŒ Don't forget to implement Unity runtime equivalents

๐Ÿ“„ License

License to use only for NON-COMMERCIAL PROJECTS.


Happy Dreamcast Development! ๐ŸŽฎโœจ

About

Transpiller to convert C# Scripts to C++ for SEGA Dreamcast

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages