forked from secretshipfic/CardMachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameGen.py
180 lines (156 loc) · 7.18 KB
/
GameGen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'''
Master Game Gen
1.1
'''
import os, glob
import PIL_Helper
import argparse
from OS_Helper import Delete, CleanDirectory, BuildPage, BuildBack
from PIL import Image
from sys import exit
import json
def main(folder="TSSSF", filepath="Core Deck 1.1.6/cards.json", watermark="", generate_pdf=True):
'''
@param folder: The base game folder where we'll be working.
E.g. TSSSF, BaBOC
@param filepath: The filepath (relative to the base folder) where the
file that defines the different cards in the game are stored.
'''
CardFile = open(os.path.join(folder, filepath))
# Read first line of file to determine format and/or module
first_line = CardFile.readline()
seond_line = ''
third_line = ''
if first_line == "{\n":
file_type = 'json'
# Load Card File
CardFile.seek(0)
data = json.load(CardFile)
CardFile.close()
module_name = data['deck']['module']
cards = data['cards']
else:
file_type = 'pon'
if first_line == "TSSSF_CardGen":
print 'Warning: .pon files are DEPRECATED for TSSSF. Support for this format may be removed soon. Please use the pontojson.py converter to convert this file to JSON format.'
module_name = first_line
pos = CardFile.tell()
# Attempt to read extra metadata
metadata = CardFile.readline()
CardFile.seek(pos)
# Load Card File and strip out comments
cards = [line for line in CardFile if not line[0] in ('#', ';', '/')]
CardFile.close()
try:
module = __import__(module_name.strip())
except ValueError:
print "Failed to load module: " + str(ValueError)
return
card_set = os.path.dirname(filepath)
if file_type == 'json':
if data['deck'].get('version', '') != '':
card_set_text = '{} {}'.format(data['deck']['name'], data['deck']['version'])
else:
card_set_text = data['deck']['name']
module.CardSet = card_set_text
module.DEFAULT_ARTIST = data['deck'].get('defaultArtist', module.DEFAULT_ARTIST)
if 'symbol' in data['deck']:
module.GlobalExpansionIcon = data['deck']['symbol']
else:
if metadata[0] == '#':
metadata_dict = metadata[1:].strip('\n').strip('\r').replace(r'\n', '\n').split('`')
card_set_name = metadata_dict[0]
card_set_version = metadata_dict[1] or ''
default_artist_name = metadata_dict[2] or module.DEFAULT_ARTIST
expansion_symbol = metadata_dict[3] or None
if card_set_version != '':
card_set_text = '{} {}'.format(card_set_name, card_set_version)
else:
card_set_text = card_set_name
module.CardSet = card_set_text
module.DEFAULT_ARTIST = default_artist_name
if expansion_symbol is not None:
module.GlobalExpansionIcon = expansion_symbol
else:
module.CardSet = os.path.dirname(filepath)
module.card_set = module.CardSet
module.CardPath = os.path.join(folder, card_set, "art")
module.OverlayPath = os.path.join(folder, card_set, "overlay")
module.ExpansionIconsPath = os.path.join(folder, card_set, "expansion-icon")
if watermark != "":
module.WatermarkImage = os.path.join(folder, "watermarks", watermark)
# Create workspace for card images
workspace_path = CleanDirectory(path=folder, mkdir="workspace", rmstring="*.*")
# Create image directories
bleed_path = CleanDirectory(path=folder + "/" + card_set, mkdir="bleed-images", rmstring="*.*")
module.BleedsPath = bleed_path
cropped_path = CleanDirectory(path=folder + "/" + card_set, mkdir="cropped-images", rmstring="*.*")
module.CropPath = cropped_path
vassal_path = CleanDirectory(path=folder + "/" + card_set, mkdir="vassal-images", rmstring="*.*")
module.VassalPath = vassal_path
# Create output directory
output_folder = CleanDirectory(path=folder, mkdir=card_set, rmstring="*.pdf")
# Make pages
card_list = []
back_list = []
page_num = 0
for line in cards:
card_list.append(module.BuildCard(line))
back_list.append(module.BuildBack(line))
# If the card_list is big enough to make a page
# do that now, and set the card list to empty again
#
if len(card_list) >= module.TOTAL_CARDS and generate_pdf:
page_num += 1
print "Building Page {}...".format(page_num)
BuildPage(card_list, page_num, module.PAGE_WIDTH, module.PAGE_HEIGHT, workspace_path)
BuildBack(back_list, page_num, module.PAGE_WIDTH, module.PAGE_HEIGHT, workspace_path)
card_list = []
back_list = []
# If there are leftover cards, fill in the remaining
# card slots with blanks and gen the last page
if len(card_list) > 0 and generate_pdf:
# Fill in the missing slots with blanks
while len(card_list) < module.TOTAL_CARDS:
card_list.append(module.BuildCard("BLANK"))
back_list.append(module.BuildCard("BLANK"))
page_num += 1
print "Building Page {}...".format(page_num)
# print card_list
BuildPage(card_list, page_num, module.PAGE_WIDTH, module.PAGE_HEIGHT, workspace_path)
BuildBack(back_list, page_num, module.PAGE_WIDTH, module.PAGE_HEIGHT, workspace_path)
# Build Vassal
# module.CompileVassalModule()
# Generate PDF files if required
if generate_pdf:
print "\nCreating PDF..."
# os.system(r'convert "{}/card_*.png" "{}/{} Cards.pdf"'.format(workspace_path, output_folder, card_set))
os.system(r'convert "{}/page_*.png" "{}/{}.pdf"'.format(workspace_path, output_folder, card_set))
os.system(r'convert "{}/backs_*.png" "{}/backs_{}.pdf"'.format(workspace_path, output_folder, card_set))
print "Done!"
if __name__ == '__main__':
# To run this script, you have two options:
# 1) Run it from the command line with arguments. E.g.:
# python GameGen -b TSSSF -f "Core 1.0.3/cards.pon"
# 2) Comment out "main(args.basedir, args.set_file)" in this file
# and add a new line with the proper folder and card set
# in the arguments.
# See the main() docstring for more info on the use of the arguments
parser = argparse.ArgumentParser(prog="GameGen")
parser.add_argument('-f', '--set-file', \
help="Location of set file to be parsed",
default="")
parser.add_argument('-b', '--basedir',
help="Workspace base directory with resources output directory",
default="TSSSF")
parser.add_argument('-w', '--watermark', \
help="Specify the watermark to apply to all generated cards",
default="")
parser.add_argument('--no-pdf',
help="Do not generate PDF files",
action="store_false")
args = parser.parse_args()
set_file = args.set_file
if set_file is "":
set_file = raw_input("Please enter the location of the set you would like to generate [Core Deck/cards.pon]: ")
main(args.basedir, set_file, args.watermark, args.no_pdf)