Giter Site home page Giter Site logo

joshnygaard / paradox-reader Goto Github PK

View Code? Open in Web Editor NEW
28.0 3.0 4.0 22 KB

Python module to read Paradox .txt files

License: MIT License

Python 98.25% Shell 1.75%
paradox-interactive stellaris python json regex europa-universalis-4 crusaders-kings-2 hearts-of-iron-4 modding modding-tools

paradox-reader's Introduction

paradox-reader

Python module to read Paradox .txt files used in Crusader Kings II, Europa Universalis IV, Stellaris, and Hearts of Iron IV

What it Does

This code will read a Paradox .txt file so that it can be used within a Python script. It can also save the read .txt as a JSON file.

Why

My originally goal was to create some kind of tool for creating random Stellaris empires given a specific government type or personality. Instead of manually writing a JSON or YAML file of the criteria of each government or personality, it made sense to find a way to read the criteria straight from the relevant .txt files.

How Does it do That

Given a file this script will use regular expression substitutions to translate the .txt file text into valid JSON. The Paradox .txt file format is fairly permissive so several choices had to be made to jerry-rig it into Python:

  • Duplicate keys are turned into lists
allow = {
  is_country_type = default
  OR = {
    AND = {
      has_ethic = "ethic_fanatic_militarist"
      OR = {
        has_ethic = "ethic_spiritualist"
        has_ethic = "ethic_egalitarian"
        has_ethic = "ethic_xenophile"
      }
    }
    AND = {
      has_ethic = "ethic_fanatic_xenophile"
      has_ethic = "ethic_militarist"
    }
  }
}

Turns into:

"allow": {
  "is_country_type": "default",
  "OR": {
    "AND": [
      {
        "has_ethic": "ethic_fanatic_militarist",
        "OR": {
          "has_ethic": [
            "ethic_spiritualist",
            "ethic_egalitarian",
            "ethic_xenophile"
          ]
        }
      },
      {
        "has_ethic": [
          "ethic_fanatic_xenophile",
          "ethic_militarist"
        ]
      }
    ]
  }
}
  • Greater than and less than comparisions get turned into objects:
happened = {
  num_owned_planets > 1
}

Turns into

"num_owned_planets": {
  "value": 1,
  "operand": ">"
}

Disclaimers

I am in no way affiliated with Paradox, nor do I have any knowledge on how they use these .txt files. Therefore I make no guarantees that this will correctly translate any given file, this has only been tested on a few files from Stellaris.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.