Pol  Revision:cb584c9
uofile00.cpp
Go to the documentation of this file.
1 
6 #include "uofile.h"
7 
8 #include <cstdio>
9 #include <string>
10 #include <fstream>
11 
12 #include "../clib/fileutil.h"
13 #include "../clib/logfacility.h"
14 #include "../clib/stlutil.h"
15 #include "../plib/systemstate.h"
16 
17 namespace Pol
18 {
19 namespace Core
20 {
21 FILE* mapfile = nullptr;
22 FILE* sidxfile = nullptr;
23 FILE* statfile = nullptr;
24 FILE* verfile = nullptr;
25 FILE* tilefile = nullptr;
26 FILE* stadifl_file = nullptr;
27 FILE* stadifi_file = nullptr;
28 FILE* stadif_file = nullptr;
29 FILE* mapdifl_file = nullptr;
30 FILE* mapdif_file = nullptr;
31 
32 std::ifstream uopmapfile;
33 
34 bool open_uopmap_file( const int mapid ) {
35  std::string filepart = "map" + std::to_string( mapid ) + "LegacyMUL.uop";
36  std::string filename = Plib::systemstate.config.uo_datafile_root + filepart;
37  if ( !Clib::FileExists( filename ) ) {
38  INFO_PRINT << filepart << " not found in " << Plib::systemstate.config.uo_datafile_root
39  << ". Searching for old map[N].mul files.\n";
40  return false;
41  }
42 
43  uopmapfile.open( filename, std::ios::binary );
44  return (bool)uopmapfile;
45 }
46 
47 FILE* open_uo_file( const std::string& filename_part )
48 {
49  std::string filename = Plib::systemstate.config.uo_datafile_root + filename_part;
50  FILE* fp = fopen( filename.c_str(), "rb" );
51  if ( !fp )
52  {
53  ERROR_PRINT << "Unable to open UO datafile: " << filename << "\n"
54  << "POL.CFG specifies UODataFileRoot as '"
55  << Plib::systemstate.config.uo_datafile_root << "'. Is this correct?\n"
56  << " The following files must be present in that directory:\n"
57  << " map0.mul OR map0LegacyMUL.uop\n"
58  << " multi.idx\n"
59  << " multi.mul\n"
60  << " staidx0.mul\n"
61  << " statics0.mul\n"
62  << " tiledata.mul\n"
63  << " verdata.mul (optional - only if present on client install)\n";
64 
65  throw std::runtime_error( "Error opening UO datafile." );
66  }
67  return fp;
68 }
69 
70 FILE* open_map_file( std::string name, int map_id )
71 {
72  std::string filename;
73 
74  filename = name + Clib::tostring( map_id ) + ".mul";
75  if ( uo_mapid == 1 && !Clib::FileExists( Plib::systemstate.config.uo_datafile_root + filename ) )
76  {
77  ERROR_PRINT << "Unable to find UO file: " << filename
78  << ", reading " + name + "0.mul instead.\n";
79  filename = name + "0.mul";
80  }
81 
82  return open_uo_file( filename );
83 }
84 
85 int uo_mapid = 0;
86 int uo_usedif = 0;
87 bool uo_readuop = true;
88 
89 unsigned short uo_map_width = 6144;
90 unsigned short uo_map_height = 4096;
91 void open_uo_data_files( void )
92 {
93  std::string filename;
94 
95  // First tries to load the new UOP files. Otherwise fall back to map[N].mul files.
96  // map1 uses map0 + 'dif' files, unless there is a map1.mul (newer clients)
97  // same for staidx and statics
98 
99  if ( !uo_readuop || !open_uopmap_file( uo_mapid ) )
100  mapfile = open_map_file( "map", uo_mapid );
101 
102  sidxfile = open_map_file( "staidx", uo_mapid );
103  statfile = open_map_file( "statics", uo_mapid );
104 
105  if ( Clib::FileExists( ( Plib::systemstate.config.uo_datafile_root + "verdata.mul" ).c_str() ) )
106  {
107  verfile = open_uo_file( "verdata.mul" );
108  }
109  else
110  {
111  verfile = NULL;
112  }
113  tilefile = open_uo_file( "tiledata.mul" );
114 
115  if ( uo_usedif )
116  {
117  filename = "stadifl" + Clib::tostring( uo_mapid ) + ".mul";
118  if ( Clib::FileExists( Plib::systemstate.config.uo_datafile_root + filename ) )
119  {
120  stadifl_file = open_uo_file( filename );
121  filename = "stadifi" + Clib::tostring( uo_mapid ) + ".mul";
122  stadifi_file = open_uo_file( filename );
123  filename = "stadif" + Clib::tostring( uo_mapid ) + ".mul";
124  stadif_file = open_uo_file( filename );
125  }
126  filename = "mapdifl" + Clib::tostring( uo_mapid ) + ".mul";
127  if ( Clib::FileExists( Plib::systemstate.config.uo_datafile_root + filename ) )
128  {
129  mapdifl_file = open_uo_file( filename );
130  filename = "mapdif" + Clib::tostring( uo_mapid ) + ".mul";
131  mapdif_file = open_uo_file( filename );
132  }
133  }
134 }
135 }
136 }
SystemState systemstate
Definition: systemstate.cpp:12
Core::PolConfig config
Definition: systemstate.h:43
FILE * stadifl_file
Definition: uofile00.cpp:26
void open_uo_data_files(void)
Definition: uofile00.cpp:91
FILE * mapdif_file
Definition: uofile00.cpp:30
char * binary(unsigned int val, int nbits)
FILE * stadifi_file
Definition: uofile00.cpp:27
bool open_uopmap_file(const int mapid)
Definition: uofile00.cpp:34
bool uo_readuop
Definition: uofile00.cpp:87
int uo_mapid
Definition: uofile00.cpp:85
FILE * sidxfile
Definition: uofile00.cpp:22
FILE * open_map_file(std::string name, int map_id)
Definition: uofile00.cpp:70
std::string tostring(const Bscript::BTokenType &v)
Definition: tokens.cpp:19
FILE * mapdifl_file
Definition: uofile00.cpp:29
FILE * tilefile
Definition: uofile00.cpp:25
FILE * mapfile
Definition: uofile00.cpp:21
std::ifstream uopmapfile
Definition: uofile00.cpp:32
FILE * verfile
Definition: uofile00.cpp:24
unsigned short uo_map_height
Definition: uofile00.cpp:90
std::string name
Definition: osmod.cpp:943
FILE * open_uo_file(const std::string &filename_part)
Definition: uofile00.cpp:47
unsigned short uo_map_width
Definition: uofile00.cpp:89
int uo_usedif
Definition: uofile00.cpp:86
#define ERROR_PRINT
Definition: logfacility.h:230
std::string uo_datafile_root
Definition: polcfg.h:27
bool FileExists(const char *filename)
Definition: fileutil.cpp:118
#define INFO_PRINT
Definition: logfacility.h:223
FILE * statfile
Definition: uofile00.cpp:23
Definition: berror.cpp:12
FILE * stadif_file
Definition: uofile00.cpp:28