PageRenderTime 30ms CodeModel.GetById 10ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/src/tools/vmap4_extractor/adtfile.cpp

https://gitlab.com/tkrokli/TrinityCore_434
C++ | 227 lines | 170 code | 32 blank | 25 comment | 43 complexity | aef0ac0794ed1ef4648343203acf756b MD5 | raw file
  1/*
  2 * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
  3 * Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
  4 *
  5 * This program is free software; you can redistribute it and/or modify it
  6 * under the terms of the GNU General Public License as published by the
  7 * Free Software Foundation; either version 2 of the License, or (at your
  8 * option) any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful, but WITHOUT
 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 13 * more details.
 14 *
 15 * You should have received a copy of the GNU General Public License along
 16 * with this program. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "vmapexport.h"
 20#include "adtfile.h"
 21
 22#include <algorithm>
 23#include <cstdio>
 24
 25#ifdef WIN32
 26#define snprintf _snprintf
 27#endif
 28
 29char const* GetPlainName(char const* FileName)
 30{
 31    const char * szTemp;
 32
 33    if((szTemp = strrchr(FileName, '\\')) != NULL)
 34        FileName = szTemp + 1;
 35    return FileName;
 36}
 37
 38char* GetPlainName(char* FileName)
 39{
 40    char * szTemp;
 41
 42    if((szTemp = strrchr(FileName, '\\')) != NULL)
 43        FileName = szTemp + 1;
 44    return FileName;
 45}
 46
 47void FixNameCase(char* name, size_t len)
 48{
 49    char* ptr = name + len - 1;
 50
 51    //extension in lowercase
 52    for (; *ptr != '.'; --ptr)
 53        *ptr |= 0x20;
 54
 55    for (; ptr >= name; --ptr)
 56    {
 57        if (ptr > name && *ptr >= 'A' && *ptr <= 'Z' && isalpha(*(ptr - 1)))
 58            *ptr |= 0x20;
 59        else if ((ptr == name || !isalpha(*(ptr - 1))) && *ptr >= 'a' && *ptr <= 'z')
 60            *ptr &= ~0x20;
 61    }
 62}
 63
 64void FixNameSpaces(char* name, size_t len)
 65{
 66    for (size_t i=0; i<len-3; i++)
 67    {
 68        if(name[i] == ' ')
 69            name[i] = '_';
 70    }
 71}
 72
 73char* GetExtension(char* FileName)
 74{
 75    if (char* szTemp = strrchr(FileName, '.'))
 76        return szTemp;
 77    return NULL;
 78}
 79
 80extern HANDLE WorldMpq;
 81
 82ADTFile::ADTFile(char* filename) : ADT(WorldMpq, filename, false)
 83{
 84    Adtfilename.append(filename);
 85}
 86
 87bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY)
 88{
 89    if(ADT.isEof ())
 90        return false;
 91
 92    uint32 size;
 93
 94    string xMap;
 95    string yMap;
 96
 97    Adtfilename.erase(Adtfilename.find(".adt"),4);
 98    string TempMapNumber;
 99    TempMapNumber = Adtfilename.substr(Adtfilename.length()-6,6);
100    xMap = TempMapNumber.substr(TempMapNumber.find("_")+1,(TempMapNumber.find_last_of("_")-1) - (TempMapNumber.find("_")));
101    yMap = TempMapNumber.substr(TempMapNumber.find_last_of("_")+1,(TempMapNumber.length()) - (TempMapNumber.find_last_of("_")));
102    Adtfilename.erase((Adtfilename.length()-xMap.length()-yMap.length()-2), (xMap.length()+yMap.length()+2));
103    //string AdtMapNumber = xMap + ' ' + yMap + ' ' + GetPlainName((char*)Adtfilename.c_str());
104    //printf("Processing map %s...\n", AdtMapNumber.c_str());
105    //printf("MapNumber = %s\n", TempMapNumber.c_str());
106    //printf("xMap = %s\n", xMap.c_str());
107    //printf("yMap = %s\n", yMap.c_str());
108
109    std::string dirname = std::string(szWorkDirWmo) + "/dir_bin";
110    FILE *dirfile;
111    dirfile = fopen(dirname.c_str(), "ab");
112    if(!dirfile)
113    {
114        printf("Can't open dirfile!'%s'\n", dirname.c_str());
115        return false;
116    }
117
118    while (!ADT.isEof())
119    {
120        char fourcc[5];
121        ADT.read(&fourcc,4);
122        ADT.read(&size, 4);
123        flipcc(fourcc);
124        fourcc[4] = 0;
125
126        size_t nextpos = ADT.getPos() + size;
127
128        if (!strcmp(fourcc,"MCIN"))
129        {
130        }
131        else if (!strcmp(fourcc,"MTEX"))
132        {
133        }
134        else if (!strcmp(fourcc,"MMDX"))
135        {
136            if (size)
137            {
138                char* buf = new char[size];
139                ADT.read(buf, size);
140                char* p = buf;
141                int t = 0;
142                ModelInstanceNames = new std::string[size];
143                while (p < buf + size)
144                {
145                    std::string path(p);
146
147                    char* s = GetPlainName(p);
148                    FixNameCase(s, strlen(s));
149                    FixNameSpaces(s, strlen(s));
150
151                    ModelInstanceNames[t++] = s;
152
153                    ExtractSingleModel(path);
154
155                    p += strlen(p) + 1;
156                }
157                delete[] buf;
158            }
159        }
160        else if (!strcmp(fourcc,"MWMO"))
161        {
162            if (size)
163            {
164                char* buf = new char[size];
165                ADT.read(buf, size);
166                char* p = buf;
167                int q = 0;
168                WmoInstanceNames = new std::string[size];
169                while (p < buf + size)
170                {
171                    char* s = GetPlainName(p);
172                    FixNameCase(s, strlen(s));
173                    FixNameSpaces(s, strlen(s));
174
175                    WmoInstanceNames[q++] = s;
176
177                    p += strlen(p) + 1;
178                }
179                delete[] buf;
180            }
181        }
182        //======================
183        else if (!strcmp(fourcc,"MDDF"))
184        {
185            if (size)
186            {
187                nMDX = (int)size / 36;
188                for (int i=0; i<nMDX; ++i)
189                {
190                    uint32 id;
191                    ADT.read(&id, 4);
192                    ModelInstance inst(ADT, ModelInstanceNames[id].c_str(), map_num, tileX, tileY, dirfile);
193                }
194                delete[] ModelInstanceNames;
195                ModelInstanceNames = NULL;
196            }
197        }
198        else if (!strcmp(fourcc,"MODF"))
199        {
200            if (size)
201            {
202                nWMO = (int)size / 64;
203                for (int i=0; i<nWMO; ++i)
204                {
205                    uint32 id;
206                    ADT.read(&id, 4);
207                    WMOInstance inst(ADT, WmoInstanceNames[id].c_str(), map_num, tileX, tileY, dirfile);
208                }
209
210                delete[] WmoInstanceNames;
211                WmoInstanceNames = NULL;
212            }
213        }
214
215        //======================
216        ADT.seek(nextpos);
217    }
218
219    ADT.close();
220    fclose(dirfile);
221    return true;
222}
223
224ADTFile::~ADTFile()
225{
226    ADT.close();
227}