summaryrefslogtreecommitdiff
path: root/tools/itemcombodbconverter.py
blob: 33519646a12630b3515b054d8e3588afcbe6a2ca (plain) (blame)
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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
#
# This file is part of Hercules.
# http://herc.ws - http://github.com/HerculesWS/Hercules
#
# Copyright (C) 2019-2020 Hercules Dev Team
# Copyright (C) 2019 Asheraf
#
# Hercules is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import argparse
import json
import re
from utils import libconf
import utils.common as Tools


def ConvertFile(args):
	print(r'''//================= Hercules Database =====================================
//=       _   _                     _
//=      | | | |                   | |
//=      | |_| | ___ _ __ ___ _   _| | ___  ___
//=      |  _  |/ _ \ '__/ __| | | | |/ _ \/ __|
//=      | | | |  __/ | | (__| |_| | |  __/\__ \
//=      \_| |_/\___|_|  \___|\__,_|_|\___||___/
//================= License ===============================================
//= This file is part of Hercules.
//= http://herc.ws - http://github.com/HerculesWS/Hercules
//=
//= Copyright (C) 2019-2020 Hercules Dev Team
//=
//= Hercules is free software: you can redistribute it and/or modify
//= it under the terms of the GNU General Public License as published by
//= the Free Software Foundation, either version 3 of the License, or
//= (at your option) any later version.
//=
//= This program is distributed in the hope that it will be useful,
//= but WITHOUT ANY WARRANTY; without even the implied warranty of
//= MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//= GNU General Public License for more details.
//=
//= You should have received a copy of the GNU General Public License
//= along with this program.  If not, see <http://www.gnu.org/licenses/>.
//================= Description ===========================================
// Configurations file for status effects
//=========================================================================

combo_db: (
/**************************************************************************
 ************* Entry structure ********************************************
 **************************************************************************
{
	// ================ Mandatory fields ===============================
	Items: ["item_list"]   (string, array) list of items
	Script: <"
		Script
		(it can be multi-line)
	">
}
**************************************************************************/''')
	ItemDB = Tools.LoadDBConsts('item_db', f'{args.mode}', '../')
	with open (f'../db/{args.mode}/item_combo_db.txt') as dbfile:
		line = 0
		for entry in dbfile:
			line = line + 1
			if not entry.strip() or entry.startswith('//'):
				continue
			m = re.search(r'(^[0-9:]+),\{(.*)\}$', entry)
			if not m:
				print(f'Error: Invalid pattern in entry {entry}, line {line}, aborting..')
				exit()

			items_list = m.group(1).split(':')
			script = m.group(2)
			for item in range(len(items_list)):
				if int(items_list[item]) not in ItemDB:
					print(f'Error: invalid item {item} found in line {line}, aborting..')
					exit()
				items_list[item] = ItemDB[int(items_list[item])]

			if args.enable_jsbeautifier:
				import jsbeautifier
				opts = jsbeautifier.default_options()
				opts.indent_with_tabs = True
				opts.indent_level = 2
				script = jsbeautifier.beautify(script, opts)
			print(
f'''{{
	Items: {json.dumps(items_list)}
	Script: <"\n{script}\n\t">
}},''')
	print(')')

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description='Convert item combo db to new format')
	parser.add_argument('--mode', type=str, dest='mode', help='Define usage mode re/pre-re.')
	parser.add_argument('--enable-jsbeautifier', type=bool, dest='enable_jsbeautifier', help='Use jsbeautifier to auto format script fields.')
	parsed_args = parser.parse_args()
	ConvertFile(parsed_args)