Crossfire Server, Trunk
is_valid_types_gen.py
Go to the documentation of this file.
1 '''
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2021 The Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12 '''
13 import re
14 import sys
15 
16 # Here we will open the file specified on the command line to search for valid object types.
17 # This replaces the previous script, is_valid_types_gen.pl, since it is the only required part
18 # of the server that requires perl. The server already needs Python for gameplay stuff, so
19 # we're better off using Python than perl for dynamically building the check of valid types.
20 
21 # sys.argv[0] is the script name
22 if len(sys.argv) > 1:
23  # Initialize variables we'll need later
24  max_obj = -1
25  type = [False] * 255 # 255 is arbitrary. If we get more object types than that, then we'll need to adjust.
26  # sys.argv[1] is the file we want to open
27  with open(sys.argv[1], 'r') as file:
28  # Pre-compile the regexes we will be checking for.
29  start_type_list = re.compile(r"enum object_type")
30  type_listing_match = re.compile(r"^\W*(\w+)\s*\=\s*(\d+)\,?.*$")
31  end_type_list = re.compile(r"^(([^}]*};).*)$")
32  # While we haven't found the start, we want to only check for the start.
33  start_handling = False
34  line = file.readline()
35  # While there are lines in the file to consume:
36  while line is not None:
37  # Skip lines until we find the ones we want to parse.
38  if not start_handling:
39  found = start_type_list.search(line)
40  if found is not None:
41  # If we found it, set the flag.
42  start_handling = True
43  else:
44  found = end_type_list.search(line)
45  if found is not None:
46  break # If we found the end, then break out of the read loop.
47  found = type_listing_match.search(line)
48  if found is not None:
49  # If the field name is OBJECT_TYPE_MAX, then we note that as a special case
50  if found.group(1) == "OBJECT_TYPE_MAX":
51  max_obj = int(found.group(2))
52  else:
53  # We found an object type. second capture group is the number.
54  type[int(found.group(2))] = True
55  line = file.readline()
56 
57  # Okay. Now we produce the output.
58  print("/*****************************************")
59  print(" * This file is automatically generated! *")
60  print(" * Its contents will be overwritten on *")
61  print(" * the next build. *")
62  print(" * *")
63  print(" * is_valid_types_gen.py generates this. *")
64  print(" *****************************************/")
65  print("\n/**")
66  print(" * Checks if the specified type is a valid one for a Crossfire object.")
67  print(" *")
68  print(" * @param type value to check.")
69  print(" * @return 1 if the type is valid, 0 else.")
70  print(" */")
71  print("#include <global.h>")
72  print("#include <libproto.h>")
73  print("int is_type_valid(uint8_t type) {")
74  print(" if (type >= OBJECT_TYPE_MAX)")
75  print(" return 0;")
76  print(" switch (type) { // Listed types are invalid ones")
77  # Now we print the invalid types.
78  for i in range(1, max_obj):
79  if (type[i] != True):
80  print(" case %d:" % i)
81  print(" return 0;")
82  print(" }")
83  print(" return 1;")
84  print("}")
CFBank.open
def open()
Definition: CFBank.py:70
make_face_from_files.int
int
Definition: make_face_from_files.py:26