Skip to main content

Read, write, update .csf files of Command & Conquer: Red Alert 2 / Command & Conquer: Yuri's Revenge

Project description

ra2csf

A Python library for reading, writing, and updating .csf files from Command & Conquer: Red Alert 2 and Command & Conquer: Yuri's Revenge.

Installation

pip install ra2csf

Overview

CSF (Command String File) files contain localized text strings used in Red Alert 2 and Yuri's Revenge. This library provides simple functions to load, dump, and update these files.

Core Functions

ra2csf.load(file)

Load a CSF file and return a dictionary mapping string labels to their values.

Parameters:

  • file - Path to CSF file (str) or file-like object

Returns: dict[str, str] - Dictionary where keys are label names and values are the corresponding text strings.

Example:

import ra2csf

# Load from file path
strings = ra2csf.load("ra2.csf")
print(strings["NAME: Soviet War Factory"])
# Output: "Soviet War Factory"

# Load from file object
with open("ra2.csf", "rb") as f:
    strings = ra2csf.load(f)

ra2csf.dump(strmap, file)

Write a dictionary of strings to a CSF file.

Parameters:

  • strmap - Dictionary mapping label names (str) to text values (str)
  • file - Output path (str) or writable file-like object

Example:

import ra2csf

new_strings = {
    "NAME: Soviet War Factory": "Soviet War Factory",
    "DESC: Soviet War Factory": "Produces heavy vehicles",
    "NAME: Tesla Reactor": "Tesla Reactor",
}

ra2csf.dump(new_strings, "custom.csf")

ra2csf.update(file, strmap)

Update an existing CSF file with new or modified strings. Existing strings not in the update map remain unchanged.

Parameters:

  • file - Path to existing CSF file (str)
  • strmap - Dictionary of label names and values to add or modify

Example:

import ra2csf

# Update specific strings
ra2csf.update("ra2.csf", {
    "NAME: Soviet War Factory": "Soviet Tank Factory",
    "NAME: New Unit": "Apocalypse Tank",
})

Complete Example

import ra2csf

# Load original
strings = ra2csf.load("original.csf")

# Make modifications
strings["NAME: Tesla Coil"] = "Tesla Tower"

# Save to new file
ra2csf.dump(strings, "modified.csf")

# Or update in-place
ra2csf.update("original.csf", {
    "NAME: Tesla Coil": "Tesla Tower"
})

Advanced Usage

Loading with Extra Data

The load function handles both standard entries (with RTS label) and extra data entries (with WRTS label), but returns only the string values by default.

Custom File Objects

All functions accept either file paths or file-like objects:

import io
import ra2csf

# Using BytesIO
buffer = io.BytesIO()
ra2csf.dump({"LABEL": "Text"}, buffer)
buffer.seek(0)
loaded = ra2csf.load(buffer)

Development

# Install with dev dependencies
pip install ra2csf[dev]

# Run tests
pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ra2csf-0.0.2.tar.gz (42.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ra2csf-0.0.2-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file ra2csf-0.0.2.tar.gz.

File metadata

  • Download URL: ra2csf-0.0.2.tar.gz
  • Upload date:
  • Size: 42.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for ra2csf-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b0f5f4ca08971d3442a725cf9b5a5a31f811e2abbc813cd0a621ca77dd8751d0
MD5 55c82b2552a28ad7f7e0451bd9cd8fc4
BLAKE2b-256 d0e1b83f798e82d6bfc2c9c4422c8abff86565da20eee04a313eca8ae4555817

See more details on using hashes here.

File details

Details for the file ra2csf-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: ra2csf-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 29.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for ra2csf-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7db99744197b06879450ad5a8d55aaa995a7731cd6ee4f51f2563ce490f59a3c
MD5 9af5fe5b0b6d855e7c5c538c3a05f730
BLAKE2b-256 ecf1c1201f9a13340f9f0da063cc2738d00afc2f159d9bf4db9a36bcfdef3b37

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page