OSDN Git Service

Hengband 108 fix2 revision 4
[hengband/hengband.git] / src / script.c
1 /* File: script.c */
2
3 /* Purpose: Script interface */
4
5 #include "angband.h"
6
7 #ifdef USE_SCRIPT
8
9 #include "Python.h"
10
11 /*
12  * Execute a Python script
13  */
14 errr script_execute(char *name)
15 {
16         char buf[1024];
17
18         if (!name || !*name)
19         {
20                 msg_print("Oops!  No script given.");
21                 return (1);
22         }
23
24         /* Load a script file */
25         if (name[0] == '@')
26         {
27                 FILE *fff;
28                 int result;
29
30                 /* Skip the '@' */
31                 name++;
32
33                 /* Either run the specified file or try to start "idle" */
34                 if (name[0] != '\0')
35                 {
36                         /* Build the filename */
37                         path_build(buf, 1024, ANGBAND_DIR_SCRIPT, name);
38                 }
39                 else
40                 {
41                         /* Build the filename */
42                         path_build(buf, 1024, ANGBAND_DIR_SCRIPT, "idle.py");
43                 }
44
45                 fff = my_fopen(buf, "rw");
46
47                 result = PyRun_SimpleFile(fff, buf);
48
49                 my_fclose(fff);
50
51                 return result;
52         }
53         else
54         {
55                 return PyRun_SimpleString(name);
56         }
57
58         return (0);
59 }
60
61
62 #ifdef STATIC_PYTHON
63 extern void initeventc(void);
64 extern void initplayerc(void);
65 extern void initioc(void);
66 extern void initobjectsc(void);
67 extern void initcavec(void);
68 extern void initmonsterc(void);
69 extern void initpclassc(void);
70 extern void initpracec(void);
71 extern void initterrainc(void);
72 extern void initcommandsc(void);
73 extern void initrealmsc(void);
74 extern void initrandomc(void);
75 extern void initspellsc(void);
76 extern void initstorec(void);
77 extern void initsystemc(void);
78 #endif /* STATIC_PYTHON */
79
80
81 /*
82  * Initialize the script support
83  */
84 errr init_script(void)
85 {
86         char buf[1024];
87         char path[1024];
88         char path1[1024];
89         cptr *program_name = &argv0;
90
91         Py_SetProgramName((char*)argv0);
92
93 #ifdef __djgpp__
94         /* Set the enviroment variables */
95         setenv("PYTHONPATH", ANGBAND_DIR_SCRIPT, 1);
96         setenv("PYTHONHOME", ANGBAND_DIR_SCRIPT, 1);
97 #endif /* __djgpp__ */
98
99         /* Initialize the Python interpreter */
100         Py_Initialize();
101
102         /* Define sys.argv.  It is up to the application if you
103            want this; you can also let it undefined (since the Python
104            code is generally not a main program it has no business
105            touching sys.argv...) */
106         PySys_SetArgv(1, (char **)program_name);
107
108
109         /* Convert the script path to a nice string */
110         ascii_to_text(path, ANGBAND_DIR_SCRIPT);
111
112         /* Build the path to the Python modules */
113         path_build(buf, 1024, ANGBAND_DIR_SCRIPT, "python");
114
115         /* Convert the script path to a nice string */
116         ascii_to_text(path1, buf);
117
118         /* Add the "script" directory to the module search path */
119         sprintf(buf, "import sys; sys.path.insert(0, '%s'); sys.path.insert(0, '%s')", path1, path);
120
121         if (PyRun_SimpleString(buf) == 0)
122         {
123 #ifdef STATIC_PYTHON
124                 initeventc();
125                 initplayerc();
126                 initioc();
127                 initobjectsc();
128                 initcavec();
129                 initmonsterc();
130                 initpclassc();
131                 initpracec();
132                 initterrainc();
133                 initcommandsc();
134                 initrealmsc();
135                 initrandomc();
136                 initspellsc();
137                 initstorec();
138                 initsystemc();
139 #endif /* STATIC_PYTHON */
140
141                 if (PyRun_SimpleString("import init") == 0) return 0;
142         }
143
144         return -1;
145 }
146
147 #endif /* USE_SCRIPT */