Revision | 4e94e39cb2d6cf226d22d3ac0faff6aca4cecbe2 (tree) |
---|---|
Time | 2015-03-08 03:47:49 |
Author | Face |
Commiter | Face |
genericvessel: refactored loading method
Previous loading method did not take Orbiter's configuration settings into
account. In addition, this patch reverts the fallback to plain config file
definition.
@@ -106,13 +106,105 @@ | ||
106 | 106 | } |
107 | 107 | delete [] ani; |
108 | 108 | delete [] att_h; |
109 | - delete [] iniFile; | |
110 | 109 | delete [] joint_sel; |
111 | 110 | } |
112 | 111 | //############################################################################// |
113 | 112 | GenericVessel::GenericVessel(OBJHANDLE hObj,int fmodel):VESSEL3(hObj,fmodel){do_init();} |
114 | 113 | GenericVessel::~GenericVessel(){do_clean();} |
115 | 114 | //############################################################################// |
115 | + | |
116 | +/// <summary> | |
117 | +/// Gets the path from orbiter. This uses Orbiter's oapiOpenFile to get the file path according to configuration settings. | |
118 | +/// Using the overload without parameters, you can retrieve the pointer to the internal char array. | |
119 | +/// </summary> | |
120 | +/// <param name="path">The relative path to the file.</param> | |
121 | +/// <param name="root">One of Orbiter's defined roots to calculate the relative path from.</param> | |
122 | +/// <returns>A pointer to the internal char array hosting the result. Be aware that this array is overwritten whenever the | |
123 | +/// function is used.</returns> | |
124 | +char *GetPathFromOrbiter(const char *path=NULL, PathRoot root=ROOT) | |
125 | +{ | |
126 | + static char oapiFile[33000]; //WinAPI restricts to path length of 32767, anyway | |
127 | + if (path==NULL) return oapiFile; | |
128 | + oapiFile[33000-1]=0; | |
129 | + FILEHANDLE fh=oapiOpenFile(path, FILE_IN, root); | |
130 | + char *stack; | |
131 | + _asm mov stack, esp; | |
132 | + stack-=0x21c; //Peeking at the stack of the oapiOpenFile function to get the actual path | |
133 | + for(int i=0;i<33000-1;i++) if ((oapiFile[i]=stack[i])==0) break; | |
134 | + oapiCloseFile(fh, FILE_IN); | |
135 | + return oapiFile; | |
136 | +} | |
137 | + | |
138 | +/// <summary> | |
139 | +/// Gets the path to the genericvessel definition file for the specified vessel. | |
140 | +/// * spacecraft3.dll logic: | |
141 | +/// * Check for config/spacecraft/name.ini | |
142 | +/// * Our logic: | |
143 | +/// * Check for $CONFIG$/spacecraft/name.ini only if class is in the spacecraft folder | |
144 | +/// * Check for $CONFIG$/spacecraft/class.ini | |
145 | +/// * Assume configuration file is INI, too | |
146 | +/// </summary> | |
147 | +/// <param name="vessel">The vessel.</param> | |
148 | +/// <returns>A pointer to the internal array hosting the result.</returns> | |
149 | +char *GetPathForVessel(VESSEL *vessel) | |
150 | +{ | |
151 | + char *iniFile=GetPathFromOrbiter(); | |
152 | + char *name=vessel->GetName(); | |
153 | + | |
154 | + //Get lower case class name for case-folding issues | |
155 | + char *className=vessel->GetClassName(); | |
156 | + int k=strlen(className); | |
157 | + char *cn=new char[k+1]; | |
158 | + strcpy(cn, className); | |
159 | + _strlwr(cn); | |
160 | + | |
161 | + //Fix possible path separator mismatches | |
162 | + for(int i=0;i<k;i++) if (cn[i]=='/') cn[i]='\\'; | |
163 | + | |
164 | + FILE *test; | |
165 | + if (strncmp(cn, SPACECRAFTPATH, strlen(SPACECRAFTPATH))==0) | |
166 | + { | |
167 | + //INI file at legacy location | |
168 | + sprintf(iniFile, SPACECRAFTINI, name); | |
169 | + GetPathFromOrbiter(iniFile, CONFIG); | |
170 | + if ((test=fopen(iniFile, "r"))==NULL) | |
171 | + { | |
172 | + //SC3 INI not found - this is a severe error, as it should be there | |
173 | + sprintf(iniFile, SC3EXCEPTION, name); | |
174 | + oapiWriteLog(iniFile); | |
175 | + exit(-1); //Bail out | |
176 | + } | |
177 | + } | |
178 | + else | |
179 | + { | |
180 | + //try INI file at legacy location with class name | |
181 | + sprintf(iniFile, SPACECRAFTINI, cn); | |
182 | + GetPathFromOrbiter(iniFile, CONFIG); | |
183 | + if ((test=fopen(iniFile, "r"))==NULL) | |
184 | + { | |
185 | + //try standard configuration path | |
186 | + sprintf(iniFile, CONFIGPATH, cn); | |
187 | + GetPathFromOrbiter(iniFile, CONFIG); | |
188 | + if ((test=fopen(iniFile, "r"))==NULL) | |
189 | + { | |
190 | + //try alternate configuration path | |
191 | + sprintf(iniFile, CONFIGALTPATH, cn); | |
192 | + GetPathFromOrbiter(iniFile, CONFIG); | |
193 | + if ((test=fopen(iniFile, "r"))==NULL) | |
194 | + { | |
195 | + //vessel configuration not found - this is a severe error, as it should be there | |
196 | + sprintf(iniFile, CONFIGEXCEPTION, cn, cn); | |
197 | + oapiWriteLog(iniFile); | |
198 | + exit(-1); //Bail out | |
199 | + } | |
200 | + } | |
201 | + } | |
202 | + } | |
203 | + fclose(test); //We don't really need the handle | |
204 | + delete [] cn; | |
205 | + return iniFile; | |
206 | +} | |
207 | + | |
116 | 208 | //############################################################################// |
117 | 209 | void GenericVessel::clbkSetClassCaps(FILEHANDLE cfg) |
118 | 210 | { |
@@ -121,44 +213,10 @@ | ||
121 | 213 | int *grpc; |
122 | 214 | VECTOR3 xp,xr; |
123 | 215 | |
124 | - //Get the data block | |
125 | - char *class_name=GetClassName(); | |
126 | - char *name=GetName(); | |
127 | - char *the_name; | |
128 | - | |
129 | - in_cfg=cfg; | |
130 | - | |
131 | - //spacecraft3.dll logic: | |
132 | - //Check for config/spacecraft/name.ini | |
133 | - //Our logic: | |
134 | - //Check for config/spacecraft/name.ini | |
135 | - //Check for config/spacecraft/class.ini | |
136 | - iniFile=new char[strlen(name)+strlen(SPACECRAFTINIPATH)]; | |
137 | - sprintf(iniFile, SPACECRAFTINIPATH,name); | |
138 | - the_name=name; | |
139 | - | |
140 | - FILE *test=fopen(iniFile, "r"); | |
141 | - if (test==NULL){ | |
142 | - //try class | |
143 | - delete [] iniFile; | |
144 | - iniFile=new char[strlen(class_name)+strlen(SPACECRAFTINIPATH)]; | |
145 | - sprintf(iniFile, SPACECRAFTINIPATH,class_name); | |
146 | - the_name=class_name; | |
147 | - | |
148 | - test=fopen(iniFile, "r"); | |
149 | - if (test==NULL){ | |
150 | - //vessel configuration not found - this is a severe error, as it should be there | |
151 | - delete [] iniFile; | |
152 | - iniFile=new char[strlen(name)+strlen(class_name)+strlen(CONFIGEXCEPTION)]; | |
153 | - sprintf(iniFile,CONFIGEXCEPTION,name,class_name); | |
154 | - oapiWriteLog(iniFile); | |
155 | - exit(-1); //Bail out | |
156 | - } | |
157 | - } | |
158 | - if(test)fclose(test); | |
216 | + in_cfg=cfg; | |
159 | 217 | |
160 | 218 | //Load the configuration |
161 | - make_vessel((DWORD)(&gv_config),(DWORD)(&gv_data),iniFile,the_name); | |
219 | + make_vessel((DWORD)(&gv_config),(DWORD)(&gv_data),GetPathForVessel(this),GetClassName()); | |
162 | 220 | |
163 | 221 | //Switches |
164 | 222 | rcs_on=1; |
@@ -5,19 +5,12 @@ | ||
5 | 5 | //############################################################################// |
6 | 6 | #define CUSTOMDIRSLENGTH 1024 |
7 | 7 | //############################################################################// |
8 | -#define SPACECRAFTINIPATH ".\\Config\\Spacecraft\\%s.ini" | |
9 | -#define CONFIGDIRTAG "ConfigDir" | |
10 | -#define MESHDIRTAG "MeshDir" | |
11 | -#define TEXTUREDIRTAG "TextureDir" | |
12 | -#define HIGHTEXDIRTAG "HightexDir" | |
13 | -#define SCENARIODIRTAG "ScenarioDir" | |
14 | -#define CONFIGDIRDEFAULT ".\\Config\\" | |
15 | -#define MESHDIRDEFAULT ".\\Meshes\\" | |
16 | -#define TEXTUREDIRDEFAULT ".\\Textures\\" | |
17 | -#define HIGHTEXDIRDEFAULT ".\\Textures2\\" | |
18 | -#define SCENARIODIRDEFAULT ".\\Scenarios\\" | |
19 | -#define ORBITERCONFIG "Orbiter.cfg" | |
20 | -#define CONFIGEXCEPTION "abort: neither vessel configuration \"%s.ini\" nor \"%s.ini\" was found!" | |
8 | +#define SPACECRAFTPATH "spacecraft\\" | |
9 | +#define SPACECRAFTINI SPACECRAFTPATH "%s.ini" | |
10 | +#define CONFIGPATH "%s.cfg" | |
11 | +#define CONFIGALTPATH "Vessels\\%s.cfg" | |
12 | +#define SC3EXCEPTION "abort: vessel configuration \"%s.ini\" not found!" | |
13 | +#define CONFIGEXCEPTION "abort: neither vessel configuration \"%s.cfg\" nor \"Vessels\\%s.cfg\" was found!" | |
21 | 14 | //############################################################################// |
22 | 15 | #define ANI_NONE 0 |
23 | 16 | #define ANI_RUN 1 |