This is an older version. Version 2.2 adds a module to 'clean' a legends file of the special characters added by workflow to save settings - if these are not dealt with, Legends Viewer crashes instead of displaying the legends. This requires that these special characters are included in the script, but they cannot be put on the forums. The newer version is available from DFFD.
ECHO OFF
SETLOCAL
REM - This script is released under the GPL3 to the extent that I (PeridexisErrant) can do so. It incorporates code written by Parker147 to call his DwarfMapMaker, a script for GIMP. The GPL3 licence can be found at https://www.gnu.org/licenses/gpl.html
REM "__Process Legends Exports" version 2.1
REM set region ID, to use in rest of script, works for 1-99 inclusive, if site maps only sets "unknown region"
FOR /L %%G IN (1,1,99) DO (
IF EXIST "%CD%\*region%%G*" (
set "region#=region%%G"
goto got_region
)
)
If exist "%CD%\site_map-*.bmp" (
set "region#=unknown region"
goto got_region
)
:Error
Echo Legends Exports not found
echo.
echo For all parts of this script to work, you need to export the 'p' general information, 'x'ml legends file, and all 'd'etailed maps (hotkey 'a' for all).
echo.
echo This will create .png maps for Isoworld, a compresed archive for Legends Viewer, and if applicable use the Fantasty Map Maker too (see the extras folder)
pause
goto end
:got_region
REM gimp script section
rem check if the maps used by the GIMP script are present, and if not skip the whole bit
if not exist "%CD%\*-elw-*.bmp" goto skip_gimp_script
if not exist "%CD%\*-el-*.bmp" goto skip_gimp_script
if not exist "%CD%\*-veg-*.bmp" goto skip_gimp_script
if not exist "%CD%\*-vol-*.bmp" goto skip_gimp_script
if not exist "%CD%\*-tmp-*.bmp" goto skip_gimp_script
if not exist "%CD%\*-bm-*.bmp" goto skip_gimp_script
rem check for GIMP v2.8 and v2.6 in the usual install locations
IF EXIST "%programfiles%\GIMP-2.0\bin\gimp-console-2.8.exe" (
SET gimpLocation="%programfiles%\GIMP-2.0\bin\gimp-console-2.8.exe"
SET "scriptFolder=%programfiles%\GIMP-2.0\share\gimp\2.0\scripts\"
GOTO foundit
)
IF EXIST "%programfiles% (x86)\GIMP-2.0\bin\gimp-console-2.8.exe" (
SET gimpLocation="%programfiles% (x86)\GIMP-2.0\bin\gimp-console-2.8.exe"
SET "scriptFolder=%programfiles% (x86)\GIMP-2.0\share\gimp\2.0\scripts\"
GOTO foundit
)
IF EXIST "%programfiles%\GIMP-2.0\bin\gimp-console-2.6.exe" (
SET gimpLocation="%programfiles%\GIMP-2.0\bin\gimp-console-2.6.exe"
SET "scriptFolder=%programfiles%\GIMP-2.0\share\gimp\2.0\scripts\"
GOTO foundit
)
IF EXIST "%programfiles% (x86)\GIMP-2.0\bin\gimp-console-2.6.exe" (
SET gimpLocation="%programfiles% (x86)\GIMP-2.0\bin\gimp-console-2.6.exe"
SET "scriptFolder=%programfiles% (x86)\GIMP-2.0\share\gimp\2.0\scripts\"
GOTO foundit
)
GOTO skip_gimp_script
:foundit
IF exist "%scriptFolder%DwarfMapMaker.scm" GOTO use_DwarfMapMaker
echo.
ECHO You have GIMP installed, but not the Map Maker plugin.
echo.
SET /P ANSWER=Do you want to install the plugin (Y/N)?
if /i {%ANSWER%}=={y} (goto :answer_yes)
if /i {%ANSWER%}=={yes} (goto :answer_yes)
goto skip_gimp_script
:answer_yes
ECHO Copy "DwarfMapMaker.scm" into the scripts folder, which is about to open.
PAUSE
%SystemRoot%\explorer.exe "%scriptFolder%"
%SystemRoot%\explorer.exe "%CD%\..\LNP\extras\Dwarf Map Maker 1.0.3 (gimp script)"
SET /P ANSWER=Have you copied the plugin across yet (Y/N)?
if /i {%ANSWER%}=={y} (goto :foundit)
if /i {%ANSWER%}=={yes} (goto :foundit)
SET /P ANSWER=Press Y to try again, N to skip the Map Maker (Y/N)?
if /i {%ANSWER%}=={y} (goto :foundit)
if /i {%ANSWER%}=={yes} (goto :foundit)
goto skip_gimp_script
:use_DwarfMapMaker
rem The following is taken from the DwarfMapMaker script, by Parker147. It relies on the GIMP script he wrote.
set "mapName=Mapmaker-%region#%.bmp"
for %%i in (*-elw-*) do set water=%%~fi
for %%i in (*-el-*) do set elevation=%%~fi
for %%i in (*-veg-*) do set vegetation=%%~fi
for %%i in (*-vol-*) do set volcanism=%%~fi
for %%i in (*-tmp-*) do set temperature=%%~fi
for %%i in (*-bm-*) do set biome=%%~fi
for %%i in (mountains.bmp) do set mountains=%%~fi
for %%i in (trees.bmp) do set trees=%%~fi
for %%i in (dirt.bmp) do set dirt=%%~fi
set water=%water:\=\\%
set elevation=%elevation:\=\\%
set vegetation=%vegetation:\=\\%
set volcanism=%volcanism:\=\\%
set temperature=%temperature:\=\\%
set biome=%biome:\=\\%
set trees=%trees:\=\\%
set dirt=%dirt:\=\\%
set mountains=%mountains:\=\\%
set outputFile=%outputFile:\=\\%
start /wait "" %gimpLocation% -d -f -i -b "(create-save \"%water%\" \"%elevation%\" \"%vegetation%\" \"%volcanism%\" \"%temperature%\" \"%biome%\" \"%trees%\" \"%dirt%\" \"%mountains%\" \"%mapName%\")"
:skip_gimp_script
rem convert bitmaps to .png
if not exist "%CD%\optipng.exe" goto no_optipng
rem - The "compress-bitmaps" part, which I edited to bypass the files used by the map maker above
if exist "%CD%\*egion*.bmp" optipng -zc9 -zm9 -zs0 -f0 *egion*.bmp
if %ERRORLEVEL% == 0 del *egion*.bmp
rem addition to handle site maps:
if exist "%CD%\site_map-*.bmp" optipng -zc9 -zm9 -zs0 -f0 site_map-*.bmp
if %ERRORLEVEL% == 0 del site_map-*.bmp
:no_optipng
)
rem Compress legends with 7z, because the xml is massive
if not exist "%CD%\7z.exe" goto legends_compressed
rem - prefer an archive compatible with "Legends Viewer.exe" ...
If exist "%region#%*-legends.xml" (
if exist "%region#%*-world_history.txt" (
if exist "%region#%*-world_sites_and_pops.txt" (
if exist "world_graphic-%region#%*.*" (
echo "%region#%-legends.xml">listlegends.txt
echo "%region#%-world_history.txt">>listlegends.txt
echo "%region#%-world_sites_and_pops.txt">>listlegends.txt
echo "world_graphic-%region#%-*.*">>listlegends.txt
7z.exe a "Legends Archive for %region#%.zip" @listlegends.txt
DEL "%CD%\listlegends.txt"
DEL "%CD%\*-world_sites_and_pops.txt"
DEL "%CD%\*-world_history.txt"
DEL "%CD%\%region#%-legends*.xml"
goto legends_compressed
) else (
if exist "world_map-%region#%*.*" (
echo "%region#%-legends.xml">listlegends.txt
echo "%region#%-world_history.txt">>listlegends.txt
echo "%region#%-world_sites_and_pops.txt">>listlegends.txt
echo "world_map-%region#%*.*">>listlegends.txt
7z.exe a "Legends Archive for %region#%.zip" @listlegends.txt
DEL "%CD%\listlegends.txt"
DEL "%CD%\*-world_sites_and_pops.txt"
DEL "%CD%\*-world_history.txt"
DEL "%CD%\%region#%-legends*.xml"
goto legends_compressed
)
)
)
)
)
rem ... but just the xml if that's not possible
If exist "%region#%*-legends.xml" do (
if not exist "Legends Archive for %region#%.zip" do (
7z.exe a "%region#%-legends-xml.zip" "%region#%*-legends.xml"
DEL "%CD%\%region#%-legends*.xml"
goto legends_compressed
)
)
:legends_compressed
rem moving all the exports to the User Content folder
if not exist "%CD%\..\User Generated Content" MD "%CD%\..\User Generated Content"
rem create a region-specific folder
SET "legendsfolder=%CD%\..\User Generated Content\%region#% legends and data"
IF NOT EXIST "%legendsfolder%" MD "%legendsfolder%"
rem world maps to a 'world maps' subfolder
if exist "%CD%\*%region#%*.png" (
if not exist "%legendsfolder%\world maps" MD "%legendsfolder%\world maps"
MOVE "%CD%\*%region#%*.png" "%legendsfolder%\world maps"
)
if exist "%CD%\*%region#%*.bmp" (
if not exist "%legendsfolder%\world maps" MD "%legendsfolder%\world maps"
MOVE "%CD%\*%region#%*.bmp" "%legendsfolder%\world maps"
)
rem move legends to the region folder
if exist "%CD%\Legends Archive for %region#%.zip" MOVE "%CD%\Legends Archive for %region#%.zip" "%legendsfolder%"
if exist "%CD%\%region#%*-world_gen_param.txt" MOVE "%CD%\%region#%*-world_gen_param.txt" "%legendsfolder%"
rem ... and if a previous part of the script didn't work properly:
if exist "%CD%\%region#%-legends-xml.zip" MOVE "%CD%\%region#%-legends-xml.zip" "%legendsfolder%"
if exist "%CD%\%region#%-legends.xml" MOVE "%CD%\%region#%-legends.xml" "%legendsfolder%"
if exist "%CD%\*-world_sites_and_pops.txt" MOVE "%CD%\*-world_sites_and_pops.txt" "%legendsfolder%"
if exist "%CD%\*-world_history.txt" MOVE "%CD%\*-world_history.txt" "%legendsfolder%"
rem move site maps to a 'site maps' subfolder
if exist "%CD%\site_map-*.*" (
if not exist "%legendsfolder%\site maps" MD "%legendsfolder%\site maps"
MOVE "%CD%\site_map-*.*" "%legendsfolder%\site maps"
)
rem delete color keys
if exist "%CD%\*_color_key.txt" DEL "%CD%\*_color_key.txt"
:end
# PeridexisErrant's Legends Processing Script in Python
# a learning excercise, released under the GPL3 - feedback much appreciated
# Dependencies: optipng and 7zip for full operation. It'll work without them, but not do very much!
# GIMP and Parker47's Dwarf Map Maker plugin allow for extra functionality
import subprocess
import fnmatch
import os
import glob
import shutil
# the which-region-are-we-working-on section
try:
regionnum_str = 'Unknown region'
count = 0 # initialise count
for file in os.listdir('.'): # for all files in folder (DF folder)
for n in range (99, 0, -1): # checking down from 99 (avoids region1 string matching region11)
if fnmatch.fnmatch(file, '*region' + str(n) + '*'):
regionnum_str = 'region' + str(n) # have string 'regionnum_str' as a variable for filenaming and matching
print('script working on legends exports for ', regionnum_str) # check that it works!
count += 1
if count > 0: # only set 'regionnum_str' once - avoids proceeding to a lower numbered region
break
if regionnum_str == 'Unknown region':
if not glob.glob('site_map-*.???') == []:
regionnum_str = 'site maps from unknown region' # can be changed, must startswith('site') or modify folder section
print('script working on site maps from unknown region')
if regionnum_str == 'Unknown region':
print('There is no file present with region(number) in the filename!')
print('This script does not currently work for local site maps, though support is planned')
print('Check that legends exports are present in the folder, and try again!')
quit()
except:
print('Something went wrong identifying which region exports are from!')
# Dwarf Map Maker called here. Requires GIMP and the plugin.
try:
GIMP_installed_bool = False # initialising
if os.name == 'nt': #windows
for item in ['Program Files', 'Program Files (x86)']:
for num in ['6', '8']:
if os.path.exists('C:/' + item + '/GIMP-2.0/bin/gimp-console-2.' + num + '.exe'): # string separated as a workaround for \b == backspace
GIMP_path_str = '"C:/' + item + '/GIMP-2.0/bin/gimp-console-2.' + num + '.exe"'
GIMP_installed_bool = True
#doesn't check for plugin yet, or handle that at all. hard though, so maybe later (I tried and failed)
if os.name == 'mac': #mac
#set GIMP_path_str here
#set GIMP_installed_bool to True
print('needs GIMP install location if installed...')
if os.name == 'posix': # linux, I think. Not sure though, I'm writing this w/out internet
print("I'd love some help to upgrade this script, which currently can't run the map maker for you. Frankly, I'm not even sure this is Linux...")
if GIMP_installed_bool:
mapname_str = ' \\"Mapmaker-' + regionnum_str + '.bmp\\"'
source_image_list = ['TREES.bmp', 'DIRT.bmp', 'MOUNTAINS.bmp']
map_stub_list = ['-elw-', '-el-', '-veg-', '-vol-', '-tmp-', '-bm-']
map_name_list = []
image_path_list = []
for item in map_stub_list:
map_item_list = glob.glob('world_graphic' + item + regionnum_str + '*.bmp')
for item in map_item_list:
map_name_list.append(item)
for l in [map_name_list, source_image_list]:
for item in l:
image_path_list.append(str(os.path.abspath(item)))
called_options_str = ' -d -f -i -b "(create-save'
for image_path in image_path_list:
called_options_str += ' \\"' + str(image_path).replace('\\', '\\\\') + '\\"' # when adding Mac/Linux support, see if '/' works in paths (more direct)
called_options_str += mapname_str + ')"'
if str(len(map_name_list)) == '6':
if os.name == 'nt':
try:
subprocess.call('start /wait "Fantasy Map Maker" ' + GIMP_path_str + called_options_str, shell=True)
# this bit ("Fantasy Map Maker") is a name for the opened window - Windows shell requires something (even '""'). Could be ommited on other platforms I assume.
print('fantasy map made')
except:
print('there was a problem calling the DwarfMapMaker.scm GIMP plugin')
if os.name == 'mac':
print("You may have GIMP (and the plugin) installed, but this script can't call the map maker yet on Mac / OSX - partly because I can't test that. I'd love some help though!")
print('attempting anyway...')
try:
subprocess.call(GIMP_path_str + called_options_str, shell=True) # there must be more to this...
print('it worked!')
except:
print('failed as expected')
except:
print('there was a problem calling the DwarfMapMaker.scm GIMP plugin')
# call optipng to PNG-ify all bitmaps containing region string
try:
subprocess.call(str('optipng -zc9 -zm9 -zs0 -f0 *' + regionnum_str + '*.bmp'))
bitmaps_list = glob.glob('*' + regionnum_str + '*.bmp')
for b in bitmaps_list:
os.remove(b)
print('bitmaps compressed')
# and also all site maps:
subprocess.call('optipng -zc9 -zm9 -zs0 -f0 site_map-*.bmp')
site_bitmaps_list = glob.glob('site_map-*.bmp')
for sb in site_bitmaps_list:
os.remove(sb)
except:
print('There was a problem calling Optipng!')
# remove the colour keys, because we don't care about them
try:
color_keys_list = glob.glob('*_color_key*.txt')
for item in color_keys_list:
os.remove(item)
print('color keys removed')
except:
print('there was a problem removing colour keys')
# TO DO: section to clean xml file of non-printing characters caused by dfhack (eg for Legends Viewer)
# the file compression bit
try:
if os.path.exists(regionnum_str + '-legends.xml'):
if os.path.exists(regionnum_str + '-world_history.txt'):
if os.path.exists(regionnum_str + '-world_sites_and_pops.txt'):
basic_mapname_list = glob.glob('world_map-' + regionnum_str + '-*.*')
detailed_mapname_list = glob.glob('world_graphic-' + regionnum_str + '-*.*')
for each_list in [basic_mapname_list, detailed_mapname_list]:
for item in each_list:
mapname_str = item # this iterates through all matches; basic maps first then by alphabetical file extension which gives the order of precedence
try:
# write files to compress to text file 'listlegends.txt'
listlegends = open("listlegends.txt", 'w')
print(str(regionnum_str + '-legends.xml'), file=listlegends)
print(str(regionnum_str + '-world_history.txt'), file=listlegends)
print(str(regionnum_str + '-world_sites_and_pops.txt'), file=listlegends)
print(str(mapname_str), file=listlegends)
listlegends.close()
except:
print('the listing bit went wrong')
try:
# call 7zip to compress the files listed
subprocess.call(str('7z a "Legends Archive for ' + regionnum_str + '.zip" @listlegends.txt'))
try:
#listing files to delete
deletion_list = ['listlegends.txt', str(regionnum_str + '-legends.xml'), str(regionnum_str + '-world_history.txt'), str(regionnum_str + '-world_sites_and_pops.txt')]
for item in deletion_list:
os.remove(item)
except:
print('there was a problem removing files')
except:
print('There was a problem calling 7zip!')
print('compressed folder of legends created')
except:
print('There was a problem with the legends compression section!')
# section to move files to created folders and conditionally create subfolders
try:
if not os.path.exists('../User Generated Content'):
os.mkdir('../User Generated Content')
if not os.path.exists(regionnum_str + ' legends and data'):
os.mkdir(regionnum_str + ' legends and data')
detailed_maps_list = glob.glob('world_graphic-*' + regionnum_str + '*.???')
if not detailed_maps_list == []:
os.mkdir(regionnum_str + ' legends and data/detailed world maps')
for item in detailed_maps_list:
shutil.copy(item, str(regionnum_str + ' legends and data/detailed world maps'))
os.remove(item)
site_maps_list = glob.glob('site_map-*.???')
if not site_maps_list == []:
os.mkdir(regionnum_str + ' legends and data/site maps')
print('all site maps assumed to be for this region')
for item in site_maps_list:
shutil.copy(item, regionnum_str + ' legends and data/site maps')
os.remove(item)
other_files_list = glob.glob('*' + regionnum_str + '*.???')
for item in other_files_list:
shutil.copy(item, regionnum_str + ' legends and data')
os.remove(item)
if not os.path.exists('../User Generated Content/' + regionnum_str + ' legends and data'):
shutil.move(str(regionnum_str + ' legends and data'), '../User Generated Content/')
print('legends moved to new folder "../User Generated Content/' + regionnum_str + ' legends and data"')
else:
print('another folder of legends for ' + regionnum_str + 'is already present, move manually to avoid overwrites')
except:
print('folder creation or file movement may have failed')
# last,
print('program finished!')