source: golgotha/src/maxtool/m1_update.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 4.6 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "r1_api.hh"
10#include "lisp/li_init.hh"
11#include "status/status.hh"
12#include "m1_info.hh"
13#include "memory/array.hh"
14#include "r1_res.hh"
15#include "tupdate.hh"
16#include "error/alert.hh"
17#include "file/file.hh"
18
19w32 m1_get_file_id(const i4_const_str &fname)
20{
21  int x;
22  char st[30], *s;
23  s=st;
24 
25  i4_const_str::iterator l=fname.begin();
26  while (l!=fname.end() && l.get().ascii_value()!='.')
27  {
28    *(s++)=l.get().ascii_value();
29    ++l;
30  }
31
32  *s=0;
33
34  if (sscanf(st,"%x",&x))
35    return x;
36  else return 0;   
37}
38
39
40struct m1_texture
41{
42  w32 id;
43  w32 last_modified;
44};
45
46int m1_find_texture(i4_array<m1_texture> &t, int id)
47{
48  for (int i=0; i<t.size(); i++)
49    if (t[i].id==id)
50      return i;
51  return -1;
52}
53
54int m1_texture_compare(const m1_texture *a, const m1_texture *b)
55{
56  if (a->id < b->id)
57    return -1;
58  else
59  if (a->id > b->id)
60    return 1;
61  else
62    return 0;
63}
64
65inline char *remove_paths(char *src)
66{
67  char *ret = src;
68
69  while (*src)
70  {
71    if (*src=='/' || *src=='\\')
72      ret=src+1;
73
74    src++;   
75  }
76
77  return ret;
78}
79
80void m1_copy_update(i4_bool all)
81{
82  int i;
83
84  i4_status_class *stat=i4_create_status(i4gets("updating_textures")); 
85
86  i4_array<i4_str *> tlist(64,64);
87  m1_info.get_texture_list(tlist,all);
88
89
90  int t=tlist.size(); 
91  i4_directory_struct ds;
92  i4_get_directory(i4gets("default_tga_dir"), ds, i4_T);
93 
94  i4_array <m1_texture> network_tga_list(128,128); 
95
96  for (i=0; i<ds.tfiles; i++)
97  {
98    if (stat)
99      stat->update(i/(float)ds.tfiles);
100
101    m1_texture new_entry;   
102   
103    new_entry.id            = r1_get_texture_id(*ds.files[i]);
104    new_entry.last_modified = ds.file_status[i].last_modified;
105
106    network_tga_list.add(new_entry);
107  }
108
109  if (stat)
110    delete stat;
111
112  stat = i4_create_status(i4gets("updating_textures"), i4_T); 
113
114  i4_directory_struct ds2;
115  i4_get_directory(r1_get_compressed_dir(), ds2, i4_T);
116
117  i4_array <m1_texture> local_gtx_list(128,128);
118 
119  m1_texture temp_tex;
120
121
122  for (i=0; i<ds2.tfiles; i++)
123  {   
124    if (stat && !stat->update(i/(float)ds2.tfiles))
125    {
126      if (stat)
127        delete stat;
128      return;
129    }
130
131    temp_tex.id            = m1_get_file_id(*ds2.files[i]);
132    temp_tex.last_modified = ds2.file_status[i].last_modified;
133    local_gtx_list.add(temp_tex);
134  }
135
136  if (stat)
137    delete stat;
138  stat = i4_create_status(i4gets("updating_textures"), i4_T);
139
140  network_tga_list.sort(m1_texture_compare);
141  local_gtx_list.sort(m1_texture_compare);   
142 
143  for (i=0; i<t; i++)
144  {   
145    if (stat && !stat->update(i/(float)t))
146    {
147      if (stat)
148        delete stat;
149      return;
150    }
151
152    if (tlist[i]->null()) continue;
153
154    temp_tex.id = r1_get_texture_id(*tlist[i]);   
155
156    w32 network_index = network_tga_list.binary_search(&temp_tex,m1_texture_compare);
157    if (network_index != -1)
158    {     
159      w32 local_index = local_gtx_list.binary_search(&temp_tex,m1_texture_compare);
160
161      if (local_index==-1 ||
162          (local_gtx_list[local_index].last_modified<network_tga_list[network_index].last_modified)
163         )
164      {
165        m1_info.texture_list_changed();
166       
167        m1_update_texture(*tlist[i], i4_T,
168                          stat,
169                          i/(float)t, (i+1)/(float)t);
170      }
171    }
172    else
173    {
174      char s1[256];
175      char s2[256];
176      char tga_dir[256];
177
178      i4_os_string(*tlist[i],s1,256);
179      i4_os_string(i4gets("default_tga_dir"),tga_dir,256);
180
181      char *filename = remove_paths(s1);
182
183      sprintf(s2,"Texture Missing: %s\\%s",tga_dir,filename);
184
185      i4_str *n = i4_from_ascii(s2);
186      i4_alert(*n,256);
187      delete n;
188    }
189       
190    delete tlist[i];       
191  }
192     
193  if (stat)
194    delete stat;
195}
196
197li_object *m1_update_all_textures(li_object *o, li_environment *env)
198{
199  m1_copy_update(i4_T);
200  return 0;
201}
202li_automatic_add_function(m1_update_all_textures, "update_all_textures");
203
204li_object *m1_update_textures(li_object *o, li_environment *env)
205{
206  m1_info.texture_list_changed();
207  return 0;
208}
209li_automatic_add_function(m1_update_textures, "update_textures");
Note: See TracBrowser for help on using the repository browser.