m1_update.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include "r1_api.hh"
  9. #include "lisp/li_init.hh"
  10. #include "status/status.hh"
  11. #include "m1_info.hh"
  12. #include "memory/array.hh"
  13. #include "r1_res.hh"
  14. #include "tupdate.hh"
  15. #include "error/alert.hh"
  16. #include "file/file.hh"
  17. w32 m1_get_file_id(const i4_const_str &fname)
  18. {
  19. int x;
  20. char st[30], *s;
  21. s=st;
  22. i4_const_str::iterator l=fname.begin();
  23. while (l!=fname.end() && l.get().ascii_value()!='.')
  24. {
  25. *(s++)=l.get().ascii_value();
  26. ++l;
  27. }
  28. *s=0;
  29. if (sscanf(st,"%x",&x))
  30. return x;
  31. else return 0;
  32. }
  33. struct m1_texture
  34. {
  35. w32 id;
  36. w32 last_modified;
  37. };
  38. int m1_find_texture(i4_array<m1_texture> &t, int id)
  39. {
  40. for (int i=0; i<t.size(); i++)
  41. if (t[i].id==id)
  42. return i;
  43. return -1;
  44. }
  45. int m1_texture_compare(const m1_texture *a, const m1_texture *b)
  46. {
  47. if (a->id < b->id)
  48. return -1;
  49. else
  50. if (a->id > b->id)
  51. return 1;
  52. else
  53. return 0;
  54. }
  55. inline char *remove_paths(char *src)
  56. {
  57. char *ret = src;
  58. while (*src)
  59. {
  60. if (*src=='/' || *src=='\\')
  61. ret=src+1;
  62. src++;
  63. }
  64. return ret;
  65. }
  66. void m1_copy_update(i4_bool all)
  67. {
  68. int i;
  69. i4_status_class *stat=i4_create_status(i4gets("updating_textures"));
  70. i4_array<i4_str *> tlist(64,64);
  71. m1_info.get_texture_list(tlist,all);
  72. int t=tlist.size();
  73. i4_directory_struct ds;
  74. i4_get_directory(i4gets("default_tga_dir"), ds, i4_T);
  75. i4_array <m1_texture> network_tga_list(128,128);
  76. for (i=0; i<ds.tfiles; i++)
  77. {
  78. if (stat)
  79. stat->update(i/(float)ds.tfiles);
  80. m1_texture new_entry;
  81. new_entry.id = r1_get_texture_id(*ds.files[i]);
  82. new_entry.last_modified = ds.file_status[i].last_modified;
  83. network_tga_list.add(new_entry);
  84. }
  85. if (stat)
  86. delete stat;
  87. stat = i4_create_status(i4gets("updating_textures"), i4_T);
  88. i4_directory_struct ds2;
  89. i4_get_directory(r1_get_compressed_dir(), ds2, i4_T);
  90. i4_array <m1_texture> local_gtx_list(128,128);
  91. m1_texture temp_tex;
  92. for (i=0; i<ds2.tfiles; i++)
  93. {
  94. if (stat && !stat->update(i/(float)ds2.tfiles))
  95. {
  96. if (stat)
  97. delete stat;
  98. return;
  99. }
  100. temp_tex.id = m1_get_file_id(*ds2.files[i]);
  101. temp_tex.last_modified = ds2.file_status[i].last_modified;
  102. local_gtx_list.add(temp_tex);
  103. }
  104. if (stat)
  105. delete stat;
  106. stat = i4_create_status(i4gets("updating_textures"), i4_T);
  107. network_tga_list.sort(m1_texture_compare);
  108. local_gtx_list.sort(m1_texture_compare);
  109. for (i=0; i<t; i++)
  110. {
  111. if (stat && !stat->update(i/(float)t))
  112. {
  113. if (stat)
  114. delete stat;
  115. return;
  116. }
  117. if (tlist[i]->null()) continue;
  118. temp_tex.id = r1_get_texture_id(*tlist[i]);
  119. w32 network_index = network_tga_list.binary_search(&temp_tex,m1_texture_compare);
  120. if (network_index != -1)
  121. {
  122. w32 local_index = local_gtx_list.binary_search(&temp_tex,m1_texture_compare);
  123. if (local_index==-1 ||
  124. (local_gtx_list[local_index].last_modified<network_tga_list[network_index].last_modified)
  125. )
  126. {
  127. m1_info.texture_list_changed();
  128. m1_update_texture(*tlist[i], i4_T,
  129. stat,
  130. i/(float)t, (i+1)/(float)t);
  131. }
  132. }
  133. else
  134. {
  135. char s1[256];
  136. char s2[256];
  137. char tga_dir[256];
  138. i4_os_string(*tlist[i],s1,256);
  139. i4_os_string(i4gets("default_tga_dir"),tga_dir,256);
  140. char *filename = remove_paths(s1);
  141. sprintf(s2,"Texture Missing: %s\\%s",tga_dir,filename);
  142. i4_str *n = i4_from_ascii(s2);
  143. i4_alert(*n,256);
  144. delete n;
  145. }
  146. delete tlist[i];
  147. }
  148. if (stat)
  149. delete stat;
  150. }
  151. li_object *m1_update_all_textures(li_object *o, li_environment *env)
  152. {
  153. m1_copy_update(i4_T);
  154. return 0;
  155. }
  156. li_automatic_add_function(m1_update_all_textures, "update_all_textures");
  157. li_object *m1_update_textures(li_object *o, li_environment *env)
  158. {
  159. m1_info.texture_list_changed();
  160. return 0;
  161. }
  162. li_automatic_add_function(m1_update_textures, "update_textures");