Mass editing jsons - Python

Follow this forum if u are a modder (having the modded version)
Contains announcements and how-to guides
Useful for forumers who want to learn how to help out in the game
Post Reply
Stratego (dev)
Site Admin
Posts: 14557
Joined: Fri Apr 25, 2014 9:28 pm

Mass editing jsons - Python

Post by Stratego (dev) » Tue Aug 31, 2021 7:47 am

I have opened this topic to "teach"/FYI anyone who is interested the power of python scripts to modify multiple files with complex logics if needed.
so all that you can not do with mass replace feature (written here: viewtopic.php?f=205&t=6120)

Example: Upscaling trigger values in maps

this example upscales all AOF map json triggers in "test" folder so all trigger effect affecting armor/hp/power will be upscaled as we did for units on the end of 2020.

here the short python code

Code: Select all

from os import listdir
from os.path import isfile, join
import json

path_work="test/"
path_workout="output/"

onlyfiles = [f for f in listdir(path_work) if isfile(join(path_work, f))]

for filename in onlyfiles :
    with open(path_work+filename) as f:
      data = json.load(f)
      if 'triggers' in data: 
        was=False
        for p_triggers in data['triggers']:
          if 'conditionsOrEffects' in p_triggers: 
            for p_conditionsOrEffects in p_triggers['conditionsOrEffects']:
              if p_conditionsOrEffects['typeID'] == 1020: 
                p_conditionsOrEffects['int1'] = p_conditionsOrEffects['int1'] * 3; 
                was=True
              if p_conditionsOrEffects['typeID'] == 1019: 
                p_conditionsOrEffects['int1'] = p_conditionsOrEffects['int1'] * 4; 
                was=True
              if p_conditionsOrEffects['typeID'] == 1031: 
                p_conditionsOrEffects['int1'] = p_conditionsOrEffects['int1'] * 4; 
                was=True
              if p_conditionsOrEffects['typeID'] == 1028: 
                p_conditionsOrEffects['int1'] = p_conditionsOrEffects['int1'] * 2; 
                was=True
              if p_conditionsOrEffects['typeID'] == 1029: 
                p_conditionsOrEffects['int1'] = p_conditionsOrEffects['int1'] * 2; 
                was=True
        if was:  
          print("was change:" + filename)
          f_out = open(path_workout+filename, 'w')
          f_out.write(json.dumps(data, separators=(',', ':')))         
print("all files written\n")


Example: Mass setting unit sight based on speed value
We needed to update all units in AOMW to have sight to be at least as movement value.

i used this script that updates file where sight < movement speed. and it set the sight to movement speed.

Code: Select all

from os import listdir
from os.path import isfile, join
import json

path_units="units/"
path_units_out="units/out/"

onlyfiles = [f for f in listdir(path_units) if isfile(join(path_units, f))]

for filename in onlyfiles :
    with open(path_units+filename) as f:
      replace_string = ""
      replace_to_string = ""
      data = json.load(f)
      if 'unitStatSheet' in data: 
        if 'unit' in data['unitStatSheet']:
          if 'rangeWalk' in data['unitStatSheet']['unit'] and 'sight' in data['unitStatSheet']['unit']:  
            if data['unitStatSheet']['unit']['rangeWalk']>data['unitStatSheet']['unit']['sight']:
              replace_string = "\"sight\":"+str(data['unitStatSheet']['unit']['sight'])
              replace_to_string = "\"sight\":"+str(data['unitStatSheet']['unit']['rangeWalk'])
      if replace_string != "":
        fin = open(path_units+filename, "rt")
        tempFile = open(path_units_out+filename, "wt")
        for line in fin:
          tempFile.write( line.replace( replace_string, replace_to_string ) )
        tempFile.close()
        print("done: " + filename)
      
print("ENDing\n")


User avatar
Maxbirykov2004
Posts: 1022
Joined: Mon Mar 30, 2020 12:50 pm
Location: Belarus

Re: Mass editing jsons - Python

Post by Maxbirykov2004 » Tue Aug 31, 2021 3:58 pm

:shock: Thats exactly what I'd like to use

User avatar
makazuwr32
Posts: 7682
Joined: Tue Oct 17, 2017 9:29 am
Location: Moscow, Russia

Re: Mass editing jsons - Python

Post by makazuwr32 » Tue Aug 31, 2021 7:21 pm

@Savra might be needed for ya.
makazuwr32 wrote:
Mon Sep 16, 2019 7:54 am
When you ask to change something argument why...
Put some numbers, compare to what other races have and so on...
© by Makazuwr32™.
AoF Dev Co-Leader
Image

Stratego (dev)
Site Admin
Posts: 14557
Joined: Fri Apr 25, 2014 9:28 pm

Re: Mass editing jsons - Python

Post by Stratego (dev) » Sat Jan 08, 2022 11:09 am

new script exxample: Example: Mass setting unit sight based on speed value

Post Reply

Return to “Modders lounge”